filmov
tv
Lesson - 26 : Hibernate - Inheritance Table per Sub-class XML and Annotation Configuration

Показать описание
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 Sub class:
1. In table per subclass for each class of hierarchy their exist a separate table in the database.
2. While creating the database tables foreign key relationship is required between parent table and child table.
3. To pass information to the hibernate that apply table per sub class mapping. We configure <joined-subclass> tag under <class> tah of hbm file.
4. In table per sub class mapping also discriminator column is optional.
Configuration:
<key column="PID"/>
<property name="cardType" column="CCTYPE"></property>
</joined-subclass>
<key column="PID"/>
<property name="chequeType" column="CHTYPE"></property>
</joined-subclass>
Hibernate – Inheritance mapping in Hibernate Table Per Sub Class using Annotations:
1. In table per sub class, discriminator is optional so we can avoid discriminator related annotations.
2. In table per sub class we need a separate table for base class and for each derived class.
3. To get a relation between base class table and derived class table we use a foreign key column in derived class table.
4. Foreign key column in derived class table can also be used as a primary key column for that derived class table to inform the hibernate that a column of a table is acting as both primary key and foreign key we use @PrimaryKeyColumn annotation.
Configuration:
@Entity
@Table(name="payments")
@Inheritance(strategy=InheritanceType.JOINED)
public class Payment {
}
@Entity
@Table(name="creditTable")
@PrimaryKeyJoinColumn(name="pid")
public class CreditCardPayment extends Payment {
}
@Entity
@Table(name="chequeTable")
@PrimaryKeyJoinColumn(name="pid")
public class ChequePayment extends Payment {
}
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 Sub class:
1. In table per subclass for each class of hierarchy their exist a separate table in the database.
2. While creating the database tables foreign key relationship is required between parent table and child table.
3. To pass information to the hibernate that apply table per sub class mapping. We configure <joined-subclass> tag under <class> tah of hbm file.
4. In table per sub class mapping also discriminator column is optional.
Configuration:
<key column="PID"/>
<property name="cardType" column="CCTYPE"></property>
</joined-subclass>
<key column="PID"/>
<property name="chequeType" column="CHTYPE"></property>
</joined-subclass>
Hibernate – Inheritance mapping in Hibernate Table Per Sub Class using Annotations:
1. In table per sub class, discriminator is optional so we can avoid discriminator related annotations.
2. In table per sub class we need a separate table for base class and for each derived class.
3. To get a relation between base class table and derived class table we use a foreign key column in derived class table.
4. Foreign key column in derived class table can also be used as a primary key column for that derived class table to inform the hibernate that a column of a table is acting as both primary key and foreign key we use @PrimaryKeyColumn annotation.
Configuration:
@Entity
@Table(name="payments")
@Inheritance(strategy=InheritanceType.JOINED)
public class Payment {
}
@Entity
@Table(name="creditTable")
@PrimaryKeyJoinColumn(name="pid")
public class CreditCardPayment extends Payment {
}
@Entity
@Table(name="chequeTable")
@PrimaryKeyJoinColumn(name="pid")
public class ChequePayment extends Payment {
}