0

我想使用 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 添加另一个字段,例如“名称”,它会起作用。但我只想有一个只包含一个集合的类。那可能吗?

4

2 回答 2

3

如果我向 Person 添加另一个字段,例如“名称”,它会起作用。但我只想有一个只包含一个集合的类。那可能吗?

不幸的是,答案是否定的。该Pet集合不是Person表中的字段,因此您基本上是在尝试不插入任何字段,Person因为唯一的字段是自动生成的 id。

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

于 2013-02-18T18:06:46.783 回答
0

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

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

于 2013-02-18T17:11:58.143 回答