创建表ActivityLog时,我在ORM Lite上收到此错误:
10-23 04:06:32.255: E/com.timelord.dao.DatabaseHelper(1487):
Caused by: java.sql.SQLException: ORMLite can't store unknown class class
com.timelord.pojo.Category for field 'category'. Serializable fields must
specify dataType=DataType.SERIALIZABLE
我怀疑这是由多级外键引起的。正如您在下面的POJO类中看到的,ActivityLog具有Activity,而Activity具有Category。
活动和类别工作正常。
有关如何进行正确映射的想法吗?
@DatabaseTable(tableName = "activityLogs")
public class AcitvityLog implements Serializable {
private static final long serialVersionUID = 1L;
@DatabaseField(generatedId = true)
private int id;
@DatabaseField(canBeNull = false)
@DatabaseFieldForeign(foreign = true)
private Activity activity;
@DatabaseField(dataType = DataType.DATE, canBeNull = false)
private Timestamp start;
@DatabaseField(dataType = DataType.DATE, canBeNull = true)
private Timestamp end;
}
@DatabaseTable(tableName = "activities")
public class Activity extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@DatabaseField(canBeNull = false)
@DatabaseFieldForeign(foreign = true)
private Category category;
}
@DatabaseTable(tableName = "categories")
public class Category extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
}
public class BaseEntity {
@DatabaseField(generatedId = true)
private Integer id;
@DatabaseField(canBeNull = false)
private String name;
}
答案 0 :(得分:2)
不,ORMLite可以处理多级外键而不会出现任何问题。问题是您无法混合@DatabaseField
和@DatabaseFieldForeign
注释。你想要的是:
@DatabaseField(canBeNull = false, foreign = true)
private Activity activity;
或者,如果您想使用在Android下运行速度更快的较小注释:
@DatabaseFieldSimple(canBeNull = false)
@DatabaseFieldForeign(foreign = true)
您使用 @DatabaseField
或@DatabaseFieldSimple
以及其他@DatabaseField...
注释。以下是@DatabaseFieldSimple
的文档:
http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/field/DatabaseFieldSimple.html