21 Jul

JPA Inheritance Strategy : Table Per Concrete Class Hierarchy (TABLE_PER_CLASS)

JPA comes with a provision to create tables and populate them as per the Java classes involved in inheritance. JPA offers basically three different approaches to map hierarchical classes – classes involved in inheritance with database tables.

Hibernate inheritance example

Observe, in the above hierarchy, three classes are involved where Person is the super class and Student and Teacher are sub-classes with their own properties declared as instance variables. Now the question is how many tables are required and moreover how to link the tables so that Student gets three properties of id and name(from super class), year.

You may read also JPA Inheritance Strategy : Table Per Class Hierarchy (SINGLE_TABLE)and JPA Inheritance Strategy : Table Per Sub-Class Hierarchy (JOINED) and Inheritance Strategy in Hibernate

JPA support three types of inheritance strategies: SINGLE_TABLE, JOINED_TABLE, and TABLE_PER_CONCRETE_CLASS.

I am going to give explanation and example on JPA table per concrete class hierarchy (TABLE_PER_CLASS), consider we have base class named Person and two derived classes – Student and Teacher.

In Table Per Concrete Class Hierarchy will have the number of tables in the database equals to the number of derived classes. Once we save the derived class object, then derived class data and base class data will be saved in the derived class related table in the database. We need the tables only for derived classes.

Read More

20 Jul

JPA Inheritance Strategy : Table Per Sub-Class Hierarchy (JOINED)

JPA comes with a provision to create tables and populate them as per the Java classes involved in inheritance. JPA offers basically three different approaches to map hierarchical classes – classes involved in inheritance with database tables.

Hibernate inheritance example

Observe, in the above hierarchy, three classes are involved where Person is the super class and Student and Teacher are sub-classes with their own properties declared as instance variables. Now the question is how many tables are required and moreover how to link the tables so that Student gets three properties of id and name(from super class), year.

You may read also JPA Inheritance Strategy : Table Per Class Hierarchy (SINGLE_TABLE)and JPA Inheritance Strategy : Table Per Concrete Class Hierarchy (TABLE_PER_CLASS) and Inheritance Strategy in Hibernate

JPA support three types of inheritance strategies: SINGLE_TABLE, JOINED_TABLE, and TABLE_PER_CONCRETE_CLASS.

I am going to give explanation and example on JPA table per sub-class hierarchy (JOINED), consider we have base class named Person and two derived classes – Student and Teacher

In Table Per SubClass Hierarchy there will be the number of classes equals to the number of tables in the database.
If we save the Student class object, hibernate will first save the data related to super class object into the super class related table in the database and then Student object data in Student related table in the database.

Read More

19 Jul

JPA Inheritance Strategy : Table Per Class Hierarchy (SINGLE_TABLE)

JPA comes with a provision to create tables and populate them as per the Java classes involved in inheritance. JPA offers basically three different approaches to map hierarchical classes – classes involved in inheritance with database tables.

Hibernate inheritance example

Observe, in the above hierarchy, three classes are involved where Person is the super class and Student and Teacher are sub-classes with their own properties declared as instance variables. Now the question is how many tables are required and moreover how to link the tables so that Student gets three properties of id and name(from super class), year.

You may read also JPA Inheritance Strategy : Table Per Concrete Class Hierarchy (TABLE_PER_CLASS) and JPA Inheritance Strategy : Table Per Sub-Class Hierarchy (JOINED) and Inheritance Strategy in Hibernate

JPA support three types of inheritance strategies: SINGLE_TABLE, JOINED_TABLE, and TABLE_PER_CONCRETE_CLASS.

I am going to give explanation and example on JPA table per class hierarchy (SINGLE_TABLE), consider we have base class named Person and two derived classes – Student and Teacher

If we save the derived class object like Student or Teacher then automatically Person class object will also be saved into the database, and in the database all the data will be stored into a single table only, which is base class table for sure.

Read More

14 Jun

Inheritance strategy in Hibernate

Hibernate comes with a provision to create tables and populate them as per the Java classes involved in inheritance. Hibernate offers basically three different approaches to map hierarchical classes – classes involved in inheritance with database tables. Read More

14 Jun

Hibernate Inheritance strategy: Table Per Class Hierarchy

I am going to give explaination and example on hibernate table per class hierarchy, consider we have base class named Person and two derived classesStudent and Teacher

If we save the derived class object like Student or Teacher then automatically Person class object will also be saved into the database, and in the database all the data will be stored into a single table only, which is base class table for sure.

But here we must use one extra discriminator column in the database to identify which derived class object we have  saved in the table along with the base class object. This discriminator column is mandatory otherwise hibernate will throw an exception. Read More

14 Jun

Hibernate Inheritance strategy: Table Per SubClass Hierarchy

In Table Per SubClass Hierarchy there will be the number of classes equals to the number of tables in the database.
If we save the Student class object, hibernate will first save the data related to super class object into the super class related table in the database and then Student object data in Student related table in the database. Read More

14 Jun

Hibernate Inheritance strategy: Table Per Concrete Class Hierarchy

In Table Per Concrete Class Hierarchy will have the number of tables in the database equals to the number of derived classes. Once we save the derived class object, then derived class data and base class data will be saved in the derived class related table in the database. We need the tables only for derived classes. For this in the hibernate mapping file we need to use one new element <union-subclass/> under <class/> tag. Read More

Share