Search code examples
javahibernatejpahibernate-mapping

Inheritance Hibernate/JPA multiple levels


I have problems with defining a schema with multiple level inheritance, so in my case I have a schema like bellow :

Model(id,created)

UserModel extends Model (login,password)

CustomerModel extends UserModel (orders)

StoreOwnerModel extends UserModel (stores)

ProductModel extends Model(name,price)

I have set inheritance strategy in Model on TABLE_PER_CLASS, so that means that I want for each sub class of Model create a table.

and the inheritance strategy in UserModel is set to SINGLE_TABLE to have just one table for all UserModel's subClasses.

But in my data base I see that for each UserModel subclasses a table is generated.

and I can't find the DiscriminatorColumn user_type in the generated table table USERS corresponding to UserModel.

here are my entities:

Model.class

@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@MappedSuperclass
public abstract class Model {

    @Id
    @GeneratedValue(strategy=GenerationType.TABLE)
    private Integer id;

    @DateTimeFormat(pattern="dd/MM/yyyy hh:mm:ss")
    private Date created;

    //getters/setters
}

UserModel.class

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="user_type",discriminatorType=DiscriminatorType.STRING)
@SecondaryTable(name="USERS")
@DiscriminatorValue("user")
public class UserModel extends Model{


    @Column(unique=true)
    private String login;
    //getters & setters
}

CustomerModel.class

@Entity
@DiscriminatorValue(value="customer")
public class CustomerModel extends UserModel{

    private List<OrderModel> orders;

    //getters & setters

}

StoreOwnerModel.class

@Entity
@DiscriminatorValue(value="store_owner")
public class StoreOwnerModel extends UserModel{

    private List<StoreModel> stores;

    //getters & setters

}

ProductModel.class

@Entity
public class StoreOwnerModel extends UserModel{

    private String name;
    private double price;
    //getters & setters

}

PS: this is not a duplucated Question, I dont Find this Answer on any of previous ones.


Solution

  • according to @chris I should remove @Inheritance from Model entity and I also removed @SecondaryTable(name="USERS") from UserModel and it worked just perfectly.