Fixus
Fixus

Reputation: 4641

how to properly extend class from abstract class

Hello I have something like this

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Person extends Model {
@Id  
public int id;

public String name;

public String lastName;
}

and

@Entity
public class User extends Person {
public Date registrationDate;

@ManyToMany(mappedBy="holders")
public List<Loan> loans = new ArrayList<Loan>();

public static Finder<Integer, User> find = new Finder<Integer, User>(Integer.class, User.class);
}

First of all. The table is created as I wanted and this is ok but I have two problems

First of all I cant make something like this

User user = new User();
user.name = "Dawid";
user.save();

I can't do that cause I get error that dtype can't be null but I can't set it (or I just don't know how to do this)

The second thing is that if I add the user from db I can't get him by id when I do that I get sql error cause the where clausule looks like this

WHERE t0. AND t0.id = 1

as you can see there is t0. and nothing after dot and this ruins everything and I don't know what it is doing there

Upvotes: 4

Views: 1158

Answers (1)

ndeverge
ndeverge

Reputation: 21564

For information, don't use User as the table name since it is a keyword in many database server:

@Entity
@Table(name = "myUserTable")
public class User extends Person {
...
}

But it is not your problem. AFAIK, Ebean supports only the SINGLE_TABLE strategy, so you'll have to use this strategy, and you can specify the discriminator (ie dtype) name on subclasses using the @DiscriminatorValue annotation:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Person extends Model {
...
}

@Entity
@DiscriminatorValue("aUser")
public class User extends Person {
...
}

Upvotes: 7

Related Questions