How can I configure Hibernate inheritance mappings using Java annotations? What are the advantages of using inheritance in Annotations?
Inherited. This is a annotation for annotations.It means that subclasses of annotated classes are considered having the same annotation as their superclass.
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.
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.
Hibernate supports the three basic inheritance mapping strategies: table per class hierarchy. table per subclass. table per concrete class.
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>
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 (?, ?)
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.
This is a very general question, but I'd advise taking a look at the following resources:
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 { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With