我想使用ormlite來保存僅包含ForeignCollection的對象。這個類看起來是這樣的:Android ormlite當持久化包含ForeignCollection的類時出現SQLException
@DatabaseTable(tableName="person")
public class Person {
@DatabaseField(generatedId = true)
private long id;
@ForeignCollectionField
private ForeignCollection<Pet> pets;
public ForeignCollection<Pet> getPets() {
return pets;
}
public long getId() {
return id;
}
}
寵物類看起來是這樣的:
@DatabaseTable(tableName="pet")
public class Pet {
public static final String PERSON_ID_FIELD_NAME = "person_id";
@DatabaseField(generatedId = true)
private long id;
@DatabaseField
private String name;
@DatabaseField(foreign=true, foreignAutoRefresh=true, columnName = PERSON_ID_FIELD_NAME)
private Person person;
[getters and setters snipped..]
我堅持階級是這樣的:
Person person = new Person();
getHelper().getPersonDao().create(person);
這崩潰,以下內容:
java.sql.SQLException: Unable to run insert stmt on object [email protected]: INSERT INTO `person`() VALUES()
at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
at com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:124)
at com.j256.ormlite.stmt.StatementExecutor.create(StatementExecutor.java:394)
at com.j256.ormlite.dao.BaseDaoImpl.create(BaseDaoImpl.java:308)
at com.eniro.core.android.orm.DummyOrmTest.testPersistPerson(DummyOrmTest.java:27)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:545)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)
Caused by: java.sql.SQLException: inserting to database failed: INSERT INTO `person`() VALUES()
at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
at com.j256.ormlite.android.AndroidDatabaseConnection.insert(AndroidDatabaseConnection.java:152)
at com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:89)
... 15 more
Caused by: android.database.sqlite.SQLiteException: near ")": syntax error: , while compiling: INSERT INTO `person`() VALUES()
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:112)
at com.j256.ormlite.android.AndroidDatabaseConnection.insert(AndroidDatabaseConnection.java:140)
... 16 more
如果我a將另一個字段添加到Person,如「name」,它可以工作。但我只想要一個只包含Collection的類。那可能嗎?
所以答案就在於異常堆棧跟蹤的更深層次。我懷疑有一個「引起」的消息。也許違反約束?發佈_full_堆棧跟蹤_anyways_建議。 – Gray 2013-02-18 16:01:46
對不起,我已經用完整的堆棧跟蹤更新了這個問題。 – 2013-02-18 17:05:14
@Gray:再次感謝ORMLite框架:)真的有沒有辦法爲ORMLite持久性指定空列黑客? – 2013-02-18 17:16:37