android - greendao 中主键短时出现 ClassCastException

标签 android orm primary-key classcastexception greendao

我创建了一个只需要通过 Short 来标识的实体。

这是我生成的代码:

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

测试代码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);
}

当我运行测试时,我得到了 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)

那么,GreenDAO是否允许让short成为primary key? 而且,我该如何处理这个异常。

谢谢

更新:

数据库生成代码

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);

源属性

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");
};

类别属性

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");
};

最佳答案

来自 greenDao website :

Current primary key (PK) restrictions: Currently, entities must have a long or Long property as their primary key. This is recommended practice for Android and SQLite. greenDAO is prepared to handle any primary key scenario in the future, but not everything is implemented completely yet. To work around this issue, you can use a long primary key and use an unique index for the intended “key” properties.

你可以尝试使用这样的东西:

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

关于android - greendao 中主键短时出现 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25710566/

相关文章:

sql-server - 创建带有主键的 View ?

android - 一个屏幕上的两个 RecyclerView

java - 语音通话期间麦克风录制的音频被静音

django - 在 Django 中执行右连接

indexing - Intershop 7.10 代码生成器备用键生成 - NONUNIQUE

entity-framework - 避免贫血域模型如何与 ORM、依赖注入(inject)和可靠方法一起使用

mysql - 在这个表示住宿服务房价的 SQL 表中设置键的最佳解决方案是什么?

android - 如何通过android蓝牙与obd2建立连接

android - 我的 kivy 程序总是在 Android 上旋转屏幕

MySQL 降级复合主键以更具限制性