gpt4 book ai didi

android - ORM Lite 在创建包含多级外键的表时抛出错误

转载 作者:行者123 更新时间:2023-11-29 00:43:36 25 4
gpt4 key购买 nike

创建表 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。

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

最佳答案

不,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

关于android - ORM Lite 在创建包含多级外键的表时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7872880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com