user1451130
user1451130

Reputation: 135

Ormlite relation and inhertitance

I wonder if there is a chance to build a relation to interhited classes. The example below does not work, because it throws the error that the relation "person" does not exist. If I place the DatabaseTable Tag above the Person Class it doesn work either. Does Ormlite support a relation to inherited objects?

public abstract class Person{
    @DatabaseField(generatedId = true)
    public int id;
    @DatabaseField
    public String name;
    @DatabaseField(canBeNull = false, foreign = true)
    private School school;
 }

@DatabaseTable
 public class Student extends Person{
    @DatabaseField
    public String class;
    @DatabaseField
    public String year;
 }

 @DatabaseTable
 public class Teacher extends Person{
    @DatabaseField
    public String title;
 }

 @DatabaseTable
 public class School {
     @DatabaseField(generatedId = true)
     public int id;
     @ForeignCollectionField(eager = true)
     ForeignCollection<Person> persons;
 }

Upvotes: 2

Views: 891

Answers (2)

Vesko
Vesko

Reputation: 41

give http://code.google.com/p/compot/ a shot. It is still in beta, but it's focused on entity inheritance and is really easy to use.

Upvotes: 4

Gray
Gray

Reputation: 116908

Sorry for the late response.

Unfortunately, this won't work. The School class has to query 2 different tables to build the persons foreign collection field.

You could accomplish this with a Person table with the Student and Teacher having a foreign Person field, but ORMLite has no way to do this for you automagically.

@DatabaseTable
public class Student {
    @DatabaseField
    public String class;
    @DatabaseField
    public String year;
    @DatabaseField(foreign = true)
    public Person person;
}

Then Person would not be abstract and there would be a Person table. That's probably how Hibernate does it under the covers.

Upvotes: 0

Related Questions