2

I have an object that I call BaseObject which all classes that are persisted extend. This class has an _id field, dateCreated, dateModified, etc.... The database tables that correspond to all objects that extends this base object obviously have columns that store these values.

How can I set up these fields using annotations so all these fields are persisted to the database? Can I do this or will I need to add these fields to the POJOs directly? I have included below my base object and one of the classes that extends it.

public class BaseObject {
    private int _id;
    private String dateCreated;
    private String dateModified;
    private String createModule;
    private String modifiedModule;

    // getters and setters here ... 
}

Here's the super-class:

@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 ... 
}

Any help would be appreciated. Thank you.

Gray
  • 115,027
  • 24
  • 293
  • 354
pleasantstranga
  • 231
  • 2
  • 14

1 Answers1

3

This should work well with ORMLite as long as you annotate each of the fields in both the base-class and the super-class that you want persisted. For example, you need to need to add something like the following annotations to the BaseObject.

@DatabaseField(generatedId = true)
private int _id;
@DatabaseField
private String dateCreated;
@DatabaseField
private String dateModified;
@DatabaseField
private String createModule;
@DatabaseField
private String modifiedModule;

And then you add the following annotations to the super-class:

@DatabaseTable(tableName = "FOOD")
public class Food extends BaseObject {

    @DatabaseField
    private Integer foodGroupId;
    @DatabaseField
    private String longDescription;
Gray
  • 115,027
  • 24
  • 293
  • 354