Play Framework 2 Ebean and InheritanceType as JOINED
Asked Answered
S

2

10

After some research on Google, I haven't found anyone who has my problem that's why I'm posting it here. In my application I have three entities : User (abstract), Customer, Agency. Customer and Agency extends User. Here is the code of User :

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class User extends AbstractModel {

    @Column(unique = true)
    @NotNull
    @Email
    public String email;

    @NotNull
    public String password;

}

The problem is that the generated schema creates only one table with the fields of User, Customer and Agency which is typically the behavior with InheritanceType.SINGLE_TABLE (default).

Is there any problem using Ebean and @Inheritance annotation ? I tried InheritanceType.TABLE_PER_CLASS, it didn't work either. I've never had this problem using JPA. Can anyone help ?

Thanks a lot ;)

Settler answered 8/8, 2012 at 20:6 Comment(5)
What's wrong using InheritanceType.SINGLE_TABLE ?Hypergolic
When using SINGLE_TABLE, I have problems with NotNull columns for example. As my table will contain agencies and customers, all the fields must be nullable. For example, I want the name and the code of an agency to be NOT NULL, but I can't put the constraint on the table as a user does not have these fields.Settler
It would be good if you moved your edit to an answer and accepted that, thus making it obvious to SO readers.Trevor
@Chafik What package do you import to use @NotNull annotation?Littoral
I import javax.validation.constraints.NotNull. I don't use any of EBean annotations.Settler
S
5

I read better the documentation of EBean and limitations : http://ebean-orm.github.io/docs/mapping/jpa/

Only Single Table Inheritance

Current there is only support for single table inheritance. The other two inheritance strategies are considered Enhancement requests and will be introduced in a feature release.

Settler answered 26/11, 2012 at 14:9 Comment(0)
H
1

If you just want an email and a password in your Customer and Agency tables, you could also take a look at the @Embedded / @Embeddable annotations:

@Embeddable
public class User  {

    @Column(unique = true)
    @NotNull
    @Email
    public String email;

    @NotNull
    public String password;

}

And the Customer class (similar for Agency):

@Entity
public class Customer  {

...

    @Embedded
    public User user;
...
}
Hypergolic answered 9/8, 2012 at 20:35 Comment(4)
Tout d'abord, merci pour ton message. Mais je ne comprends pas bien le comportement que donnera ces annotations. Après avoir lu la doc, ce comportement est assez similaire à InheritanceType.TABLE_PER_CLASS non ? Ainsi mon User ne serait pas une entité mais un simple POJO, et si j'ajoute un champ à User, une colonne est ajoutée dans les tables Agency et Customer ? Le souci est que je souhaite par exemple dans mon interface d'administration, lister les users, donc à partir de là je dois marquer User comme une entité et utiliser @OneToOne non ?Settler
Sorry, translation : Firstly, thanks for your message. But I don't understand the behavior of these annotations. After reading the doc, it seems similar to InheritanceType.TABLE_PER_CLASS no ? User is not an entity but a simple POJO and if I add a field (enabled for example) to User, a column is added in Agency and Customer tables ? The problem is that I want to list Users in my administration panel to enable/disable the account for example. I must mark User as an entity and use @OneToOne right ?Settler
Yeah, if you want an User entity you have to use inheritance. The solution I suggest does not fit your need, but it is not the same as SINGLE_TABLE, because you'll get 2 distinct tables.Hypergolic
Yes I said TABLE_PER_CLASS :) I can't use Inheritance for the moment as I explained in the comment of my initial question. Thank you for your help. I'll use @OneToOne waiting for Ebean to support JOINED.Settler

© 2022 - 2024 — McMap. All rights reserved.