持久化包含ForeignCollection的类时,Android ormlite SQLException

时间:2013-02-18 13:41:34

标签: android orm ormlite foreign-collection

我想使用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添加另一个字段,例如“name”,它就可以了。但我只想要一个只包含Collection的类。这可能吗?

2 个答案:

答案 0 :(得分:3)

  

如果我向Person添加另一个字段,例如“name”,它就可以了。但我只想要一个只包含Collection的类。这可能吗?

遗憾的是答案是否定的。 Pet集合不是Person表中的字段,因此您基本上不会在Person中插入任何字段,因为唯一的字段是自动生成的ID。

所以我现在不幸地添加一个标记字段,并记录它是必要的,因为ORMLite有限制。

答案 1 :(得分:0)

然后设置集合,一切都会好的。如果您没有设置集合,它将保持为null。我认为您遇到的错误与详细描述的here中的空列黑客问题有关。

编辑实际上即使提供了一个集合,您仍然会有空值,如果在ORMLite中没有设置NullColumnHack,插入可能会失败。