What are the types of inheritence models and describe how they work like vertical inheritence and horizontal

Showing Answers 1 - 1 of 1 Answers

There are three types of inheritance mapping in hibernate

1. Table per concrete class with unions

2. Table per class hierarchy

3. Table per subclass

 

Example:

Let us take the simple example of 3 java classes.

Class Manager and Worker are inherited from Employee Abstract class.

 

 

1. Table per concrete class with unions

In this case there will be 2 tables

Tables: Manager, Worker [all common attributes will be duplicated]

 

2. Table per class hierarchy

Single Table can be mapped to a class hierarchy

There will be only one table in database called 'Employee' that will represent all the attributes required for all 3 classes.

But it needs some discriminating column to differentiate between Manager and worker;

 

3. Table per subclass

In this case there will be 3 tables represent Employee, Manager and Worker

 

Jaleel Akbar

  • Jul 12th, 2007
 

Table per concrete class using implicit
polymorphism can also be added as inheritance mapping in hibernate

  Was this answer useful?  Yes

There are three types of inheritance mapping in hibernate
1. Table per class hierarchy
2. Table per subclass
3. Table per concrete class

In hbm.xml file you need following enteries
1. For Table per class hierarchy
<class name="user" table="user">
<discriminator>      <column name="DISCRIMINATOR"/>    </discriminator>
</class>
<subclass name="address" discriminator-value="address"/>

2. One table per sub class
<class name="Person" table="TB_PERSON" polymorphism="implicit"/>
<joined-subclass name="Individual"   table="TB_INDIVIDUAL">
      <key column="id"/>
</joined-subclass>

key is the reference key in main table

3. Table per concrete class
Here mapping is same as normal
<class...
Only difference is that subclass contains all the properties of its parent so the table contains all columns from (arent+child)
e.g
<hibernate-mapping>
  <class name="eg.hibernate.mapping.dataobject.Land" table="TB_LAND" polymorphism="implicit">
    <id name="id" column="ID">
      <generator class="assigned"/>
    </id>
    <property name="description" column="DESCRIPTION" type="java.lang.String" />
    <property name="squareFeet" column="SQUARE_FEET" type="java.lang.Double"/>
  </class>

<class name="eg.hibernate.mapping.dataobject.Building" table="TB_BUILDING" polymorphism="implicit">
    <id name="id" column="ID">
      <generator class="assigned"/>
    </id>
    <property name="description" column="DESCRIPTION" type="java.lang.String" />
    <property name="address" column="ADDRESS" type="java.lang.String"/>
  </class>
</hibernate-mapping>

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions