Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance in Hibernate Annotations?

How can I configure Hibernate inheritance mappings using Java annotations? What are the advantages of using inheritance in Annotations?

like image 254
stical Avatar asked Sep 03 '09 13:09

stical


People also ask

What is inheritance annotation?

Inherited. This is a annotation for annotations.It means that subclasses of annotated classes are considered having the same annotation as their superclass.

What is inheritance in Hibernate?

Entity inheritance means that we can use polymorphic queries for retrieving all the subclass entities when querying for a superclass. Since Hibernate is a JPA implementation, it contains all of the above as well as a few Hibernate-specific features related to inheritance.

Are JPA annotations inherited?

JPA Inheritence Annotations@MappedSuperclass - This annotation is applied to the classes that are inherited by their subclasses. The mapped superclass doesn't contain any separate table. @DiscriminatorColumn - The discriminator attribute differentiates one entity from another.

How many types of inheritance are there in Hibernate?

Hibernate supports the three basic inheritance mapping strategies: table per class hierarchy. table per subclass. table per concrete class.


2 Answers

3 possible types :

1. Single table per class hierarchy strategy:

 @Entity  @Inheritance(strategy=InheritanceType.SINGLE_TABLE)  @DiscriminatorColumn(      name="planetype",   discriminatorType=DiscriminatorType.STRING      )  @DiscriminatorValue("Plane")  public class Plane { ... }   @Entity  @DiscriminatorValue("A320")  public class A320 extends Plane { ... }         <hibernate-mapping>  <subclass name="DomesticCat" extends="Cat" discriminator-value="D">       <property name="name" type="string"/>  </subclass> 
  • Pros : Simplest. No JOINs required
  • Cons : Cannot use nulls. # of columns increase with the Object graph depth.

2. Joined subclass strategy:

Database Tables

CREATE TABLE SUPER_TABLE( id_col number primary key, sup_Name varchar2(20));  CREATE TABLE  SUB_TABLE( SUP_ID primary key, sub_name varchar2(20), constraint SUB_TABLE_fk foreign key (sup_Id)  references    super_table(id_col));   @Entity @Table(name= "SUPER_TABLE") @Inheritance(strategy= InheritanceType.JOINED)     public class TestSuperClass { @Id @GeneratedValue(         strategy=GenerationType.SEQUENCE,          generator="SEQ_GEN")     @SequenceGenerator(         name="SEQ_GEN",         sequenceName="hibernate_sequence"     )     @Column(name ="id_col") private long idcol; @Column(name ="sup_name") private String supName;   @Entity  @Table(name="SUB_TABLE") @PrimaryKeyJoinColumn(name="SUP_ID")  <class name="Payment" table="PAYMENT"> <id name="id" type="long" column="PAYMENT_ID">     <generator class="native"/> </id> <property name="amount" column="AMOUNT"/> ... <joined-subclass name="CreditCardPayment" table="CREDIT_PAYMENT">     <key column="PAYMENT_ID"/>     <property name="creditCardType" column="CCTYPE"/>     ... </joined-subclass> <joined-subclass name="CashPayment" table="CASH_PAYMENT">     <key column="PAYMENT_ID"/>     ... </joined-subclass>  public class TestSubClass extends TestSuperClass{      private String sub_name;  } 

Test Module

TestSubClass sub = new TestSubClass("sub1"); sub.setSupName("supersuper"); session1.save(sub); 

SQL Generated

 Hibernate: insert into SUPER_TABLE (sup_name, id_col) values (?, ?)      Hibernate: insert into SUB_TABLE (sub_name, SUP_ID) values (?, ?) 
  • Pros : Normalized data structures.
  • Cons : JOINS are always reqd.

3. Table per concrete class strategy:

create table CREDIT_CARD( payment_id number primary key, amount number, creditCardType varchar2(2) );     @Entity   @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)   public abstract class Payment { @Id @GeneratedValue(         strategy=GenerationType.SEQUENCE,          generator="SEQ_GEN")     @SequenceGenerator(         name="SEQ_GEN",         sequenceName="hibernate_sequence"     ) @Column(name = "payment_id") private long id;  private double amount;     @Entity  @Table(name="CREDIT_CARD")  public class CreditCardPayment extends Payment { private String creditCardType;    <class name="Payment"> <id name="id" type="long" column="PAYMENT_ID">     <generator class="sequence"/> </id> <property name="amount" column="AMOUNT"/> ... <union-subclass name="CreditCardPayment" table="CREDIT_PAYMENT">     <property name="creditCardType" column="CCTYPE"/>     ... </union-subclass> 

Test module

CreditCardPayment credit = new CreditCardPayment("C",1.0); session1.save(credit); 

SQL Generated

  Hibernate: insert into CREDIT_CARD (amount, creditCardType, payment_id) values (?, ?, ?) 

There's also a @MappedSuperClass which we have used in our application.

like image 84
Achow Avatar answered Sep 21 '22 09:09

Achow


This is a very general question, but I'd advise taking a look at the following resources:

  • The documentation on how inheritance is declared via Hibernate annotations.
  • This PDF file (chapter 2 of a book on Hibernate). Page 38 forwards deals with Hibernate annotations.

But the very basic answer to your question is that you should use the @Inheritance annotation, like so:

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class Flight implements Serializable {    ... } 
like image 21
Brandon Yarbrough Avatar answered Sep 22 '22 09:09

Brandon Yarbrough