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 ;)

c4k
  • 4,270
  • 4
  • 40
  • 65
  • What's wrong using `InheritanceType.SINGLE_TABLE` ? – ndeverge Aug 09 '12 at 20:31
  • 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. – c4k Aug 09 '12 at 21:19
  • It would be good if you moved your edit to an answer and accepted that, thus making it obvious to SO readers. – allprog Nov 26 '12 at 13:06
  • @Chafik What package do you import to use `@NotNull` annotation? – pmichna Dec 16 '13 at 18:47
  • I import javax.validation.constraints.NotNull. I don't use any of EBean annotations. – c4k Dec 17 '13 at 00:18

2 Answers2

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.

Community
  • 1
  • 1
c4k
  • 4,270
  • 4
  • 40
  • 65
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;
...
}
ndeverge
  • 21,378
  • 4
  • 56
  • 85
  • 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 ? – c4k Aug 09 '12 at 21:27
  • 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 ? – c4k Aug 09 '12 at 21:33
  • 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. – ndeverge Aug 10 '12 at 05:57
  • 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. – c4k Aug 10 '12 at 10:20