开发者

Hibernate: How can I join 2 classes in one table?

开发者 https://www.devze.com 2022-12-18 17:58 出处:网络
So, I\'m pretty new to Hibernate and I have a problem. I have an abstract class (the super-class, class Super), and 5 subclasses which should use the proprieties from the class Super and add a new pr

So, I'm pretty new to Hibernate and I have a problem.

I have an abstract class (the super-class, class Super), and 5 subclasses which should use the proprieties from the class Super and add a new propriety (a new column)

So how can I do this? Should I extend the class Super from java, or it's enough to join the classes using a JPA annotation.

Here is the second problem. How can I have 1 table for 2 classes. Someone (smarter than me) told me to use the @JoinTable, but form开发者_JAVA技巧 my searches with google, I think I need to use @Inheritance(strategy=InheritanceStrategy.JOINED)

Can I use the @JoinTable too?


Yours is a case of inheritance:

  1. add the @Inheritance(stretegy=InheritanceStrategy.SINGLE_TABLE) annotation on your Super
  2. add the @DiscriminatorColumn annotation (and setting its attributes name and discriminatorType) (again on the Super)
  3. on each subclass extend the Super, and add the annotation @DiscriminatorValue, with different value for each of the subclasses.


If you are new to Hibernate, you should read its documentation. Inheritance strategies are explained here and using annotations to express inheritance strategy is explained here

0

精彩评论

暂无评论...
验证码 换一张
取 消