我想使用 ormlite 来持久化仅包含 ForeignCollection 的对象。该类如下所示:
@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;
}
}
Pet 类如下所示:
@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 com.eniro.core.android.orm.Person@b4c33b78: 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
如果我向 Person 添加另一个字段,例如“名称”,它会起作用。但我只想有一个只包含一个集合的类。那可能吗?