4

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.

Neil Stockton
11.6k3 gold badges36 silver badges29 bronze badges
asked Jun 3, 2016 at 16:05
1
  • 4
    Model isn't an entity, it is a mappedSuperclass and shouldn't even have an inheritance strategy - each entity that extends it will be its own independent entity, only sharing the mapped attributes, not the table. Try removing the inheritance tag as it must be causing an issue with your provider somehow. You can verify this by setting it to single table inheritance - the model should still will not have a table created for it. Commented Jun 3, 2016 at 16:13

1 Answer 1

2

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

answered Jun 3, 2016 at 16:37
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.