2

我有一个对象,我称之为BaseObject所有持久化的类都扩展。这个类有一个_id字段,dateCreateddateModified,等等...... 与扩展这个基础对象的所有对象相对应的数据库表显然有存储这些值的列。

如何使用注释设置这些字段,以便将所有这些字段保存到数据库中?我可以这样做还是需要将这些字段直接添加到 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 ... 
}

任何帮助,将不胜感激。谢谢你。

4

1 回答 1

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;
于 2012-07-30T20:12:44.847 回答