Lesson - 24 : Hibernate - Inheritance Table per class using XML and Annotation based Configuration

Опубликовано: 04 Октябрь 2024
на канале: Sada Learning Hub
352
2

Hibernate – Inheritance mapping in Hibernate Table Per Class using XML Configuration :
1. Hibernate is capable for storing inherited properties of an object along with its new properties in a database when an object is saved in a database.
2. In the hibernate inheritance between pojo classes are apply when multiple pojo classes of module contains some common properties.
3. In a real time application pojo classes of hibernate are designed based on database table design.
4. If more than one pojo class of hibernate is going to have common properties then those common properties are separated into pojo class called as base class and un common properties are stored into derived classes. This concept is called interface mechanism.
5. Hibernate is capable of storing the data of a class hierarchy of an application into either one table of database or into multiple tables of database based on the database design.
6. Hibernate support three types of inheritance mapping
A) Table per class
B) Table per concrete class
C) Table per sub class

Table per class:
1. This type of inheritance mapping is applied when one table in database is created for storing the data of a entire class hierarchy.
2. If table per class inheritance mapping is used then hibernate stores a string in the database apart from the data of an object for indicating which derived class object has inserted this data of a row.
3. In table per class hibernate need in additional column in the table called as a discriminator column for placing or storing the discriminator value of a sub class object.
4. To inform the hibernate that apply table per class hierarchy. we configure <sub-class> tag under <class> tag in hbm file.

Configuration:
<subclass name="com.sada.Model.CreditCardPayment" discriminator-value="card">
<property name="cardType" column="CCTYPE"></property>
</subclass>
<subclass name="com.sada.Model.ChequePayment" discriminator-value="cheque">
<property name="chequeType" column="CHTYPE"></property>
</subclass>
Hibernate – Inheritance mapping in Hibernate Table Per Class using Annotations :
1. @Inheritance(strategy=InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name=“colnameName”)
2. In table per class inheritance, discriminator column is mandatory in other two , it is optional.
3. In table per class a single able of database is used to store the objects of all derived classes of a inheritance hierarchy.
4. The following is the table per class inheritance with annotations.
5. Hear Payment is a base class and CreditCardPayment, ChequePayment are derived classes.

Configuration:
@Entity
@Table(name="PAYMENTS")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="PMODE", discriminatorType=DiscriminatorType.STRING)
public class Payment {
}

@Entity
@DiscriminatorValue(value="card")
public class CreditCardPayment extends Payment {
}

@Entity
@DiscriminatorValue(value="cheque")
public class ChequePayment extends Payment {}


Hibernate Examples Project Github Link : https://github.com/SadaLearningHub1/H...