Inheritance in ORMLite

174 views
Skip to first unread message

Francis Novello Santos

unread,
Sep 23, 2011, 11:12:58 AM9/23/11
to ORMLite Users
Im using some kind of inheritance this way:

1) Create an abstract superclass thats not an ORMLite database table
but has some database fields used by all subclasses

public abstract class Event {

@DatabaseField(columnName="id", generatedId = true)
protected int _id;

@DatabaseField(columnName="name")
protected String name;

@DatabaseField(columnName="date")
protected Date date;

@DatabaseField(columnName="type")
protected String type;

public abstract void setName(String name);
public abstract void setDate(Date date);
public abstract void setType(String type);

}

2) Create a class that inherits it and add some fields (thats a
Database Table)

@DatabaseTable(tableName="atividade")
public class Atividade extends Event {

@DatabaseField(columnName="ala")
protected String ala;

public String getAla() {
return ala;
}

public void setAla(String ala) {
this.ala = ala;
}

public Atividade(String name, Date date,
String type, String ala) {

this.setName(name);
this.setDate(date);
this.setTipo(type);
this.setAla(ala);

}

Atividade(){
}

@Override
public void setName(String name) {
this.name = name;
}

@Override
public void setDate(Date date) {
this.date = date;
}

@Override
public void setType(String type) {
this.type = type;
}

}

Diederik

unread,
Mar 12, 2012, 11:10:38 AM3/12/12
to ormlit...@googlegroups.com

I'm not sure if the previous poster actually asked a question, but I was also wondering if ORMLite supports some sort of @Superclass like mechanism.  I want to use a common base class for all our app's NameLookup tables (they all have at least a name and an ID column). 

I looked for something like this in the docs, and google, but this post is all I found.

Thanks in advance.
Diederik

jc

unread,
Mar 12, 2012, 11:38:43 AM3/12/12
to ORMLite Users
On Mar 12, 9:10 am, Diederik <diederikjhatti...@gmail.com> wrote:
> I'm not sure if the previous poster actually asked a question, but I was
> also wondering if ORMLite supports some sort of @Superclass like
> mechanism.  I want to use a common base class for all our app's NameLookup
> tables (they all have at least a name and an ID column).
>

you can create your own base class with common database fields defined
and then extend that to create your individual data objects. ORMLite
will walk the class structure to pick up all fields defined in super
classes (no need for a special annotation) and treat them all as
fields in your subclasses table. For your example, you could create
your base class with the name and ID column definitions and then
extend that for all of your data objects to avoid having to redefine
those fields in every object. For anyone concerned with the overhead
of walking the class structure, it's only done when the TableConfig
object is created and not on every select/update.
Reply all
Reply to author
Forward
0 new messages