HIbernate - Inheritance – Strategy 3 : Table Per Concrete Class Hierarchy
Hibernate Application ( Inheritance – Strategy 3 : Table Per Class Hierarchy )
Inheritance
Mapping
Hibernate
supports 3 basic inheritance mapping strategies.
1.
Table per class hierarchy
2.
Table per subclass
3. Table
per concrete class
As
per the below example there will be 3 tables and no relations to
each other.
There
is no null values in the table since tables are created per class.
Disadvantage
of this approach is, it created duplicate columns.
To
do the table map, use union-subclass element.
Suppose
you have the base Employee class, with PermanentEmployee
and ContractEmployee as subclasses.
Steps -
Step 1 - Create POJO classes ( Employee.java and PermanentEmployee, ContractEmployee sub-classes )
Step 2 - Create mapping file ( employee.hbm.xml )
Step 3 - Hibernate Configurations ( hibernate.cfg.xml )
Step 4 - Create class to do the execution ( ExecuteEmployee.java )
Step 1 - Create POJO classes
Steps -
Step 1 - Create POJO classes ( Employee.java and PermanentEmployee, ContractEmployee sub-classes )
Step 2 - Create mapping file ( employee.hbm.xml )
Step 3 - Hibernate Configurations ( hibernate.cfg.xml )
Step 4 - Create class to do the execution ( ExecuteEmployee.java )
Step 1 - Create POJO classes
Create POJO (Plain Old Java Object) classes that represents the Employee, PermanentEmployee and ContractEmployee.
Step
2 - Create mapping file
Create a mapping file “employee.hbm.xml” which maps, “employee”, “permanentemployee”, “contractemployee” tables in the database by using <union-subclass tag.
Create a mapping file “employee.hbm.xml” which maps, “employee”, “permanentemployee”, “contractemployee” tables in the database by using <union-subclass tag.
<union-subclass name="pojo.PermanentEmployee" table="permanentemployee">
..
</union-subclass>
Step
3 - Hibernate Configurations
Create
configuration file name as hibernate.cfg.xml and save it.
Step
4 - Create Execute class
Create
a ExecuteEmployee class to do the execution.
Out
put on the above application as follows.