ClassCastException when primary key in greendao

I created an object that only needs identification Short

.

Here is my generated code:

public Source(Short id, String name) {
    this.id = id;
    this.name = name;
}

      

TestCode DatabaseHelperTest.java

:

public void testInsertAndLoad(){
    Source source = new Source((short) 0, "TestSource");
    SourceDao sourceDao = daoSession.getSourceDao(); //#line 26
    sourceDao.insert(source);
    Short id = source.getId();
    assertNotNull(id);
}

      

When I run the test, I got a ClassCastException:

Running tests
Test running started
java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.Long
at de.greenrobot.dao.identityscope.IdentityScopeLong.put(IdentityScopeLong.java:31)
at de.greenrobot.dao.AbstractDao.attachEntity(AbstractDao.java:695)
at de.greenrobot.dao.AbstractDao.updateKeyAfterInsertAndAttach(AbstractDao.java:362)
at de.greenrobot.dao.AbstractDao.executeInsert(AbstractDao.java:355)
at de.greenrobot.dao.AbstractDao.insert(AbstractDao.java:293)
at com.tuanchau.DatabaseHelperTest.testInsertAndLoad(DatabaseHelperTest.java:26)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)

      

So does GreenDAO Short

make it become primary key

? And how can I handle this exception.

thank

Update:

DB Generation Code

Entity source = schema.addEntity("Source");
Entity category = schema.addEntity("Category");

source.addShortProperty("id").primaryKey().getProperty();
source.addStringProperty("name").notNull();

category.addIntegerProperty("id").primaryKey().getProperty();
category.addStringProperty("name").notNull();
Property csid = category.addLongProperty("sid").notNull().getProperty();

category.addToOne(source, csid);

      

Source properties

public static class Properties {
    public final static Property Id = new Property(0, Short.class, "id", true, "ID");
    public final static Property Name = new Property(1, String.class, "name", false, "NAME");
};

      

Category properties

public static class Properties {
    public final static Property Id = new Property(0, Integer.class, "id", true, "ID");
    public final static Property Name = new Property(1, String.class, "name", false, "NAME");
    public final static Property Sid = new Property(2, short.class, "sid", false, "SID");
};

      

+3


source to share


1 answer


From greenDao website :

Current Primary Key (PK) Constraints: Currently, objects must have a long or long property as their primary key. This is recommended for Android and SQLite. greenDAO is ready to handle any primary key scenario in the future, but not everything is fully implemented. To work around this issue, you can use a long primary key and use a unique index on the intended properties of the key.



You can try using something like this:

source.addIdProperty();
source.addShortProperty("shortId").unique().getProperty();
source.addStringProperty("name").notNull();

      

+5


source







All Articles