Pergunta

I want to use ormlite to persist an object containing only a ForeignCollection. The class looks like this:

@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;
    }
}

The Pet class looks like this:

@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..]

I'm persisting the class like this:

Person person = new Person();
getHelper().getPersonDao().create(person);

This crashes with the following:

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

If I add another field to Person, like "name", it works. But I just want to have a class containing only a Collection. Is that possible?

Foi útil?

Solução

If I add another field to Person, like "name", it works. But I just want to have a class containing only a Collection. Is that possible?

The answer unfortunately is no. The Pet collection isn't a field in the Person table so you basically are trying to insert no fields into Person because the only field there is an auto-generated id.

So I would just add a marker field for now unfortunately and document it that it is necessary because of a limitation in ORMLite.

Outras dicas

Then set the collection and everything will be all right. If you do not set collection it will remain null. I think the error you are getting is related with the null column hack issue described in detail here.

EDIT Actually even providing a collection you will still have empty values and the insertion might fail if no NullColumnHack is set in ORMLite.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top