我有一个对象叫做BaseObject
,所有持久化的类都会扩展。此类有一个_id
字段,dateCreated
,dateModified
等等。与扩展此基础对象的所有对象相对应的数据库表显然具有存储这些值的列。 / p>
如何使用注释设置这些字段,以便所有这些字段都保存到数据库中?我可以这样做,还是需要将这些字段直接添加到POJO?我已经在我的基础对象下面包含了一个扩展它的类。
public class BaseObject {
private int _id;
private String dateCreated;
private String dateModified;
private String createModule;
private String modifiedModule;
// getters and setters here ...
}
这是超级班:
@DatabaseTable(tableName = "FOOD")
public class Food extends BaseObject {
public Food(int foodGroupId, String longDescription) {
this.foodGroupId = foodGroupId;
this.longDescription = longDescription;
}
private Integer foodGroupId;
private String longDescription;
// getters and setters here ...
}
任何帮助将不胜感激。谢谢。
答案 0 :(得分:3)
只要您注释要保留的超类的基类和中的每个字段,这应该适用于ORMLite。例如,您需要向BaseObject
添加类似以下注释的内容。
@DatabaseField(generatedId = true)
private int _id;
@DatabaseField
private String dateCreated;
@DatabaseField
private String dateModified;
@DatabaseField
private String createModule;
@DatabaseField
private String modifiedModule;
然后将以下注释添加到超类:
@DatabaseTable(tableName = "FOOD")
public class Food extends BaseObject {
@DatabaseField
private Integer foodGroupId;
@DatabaseField
private String longDescription;