Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping multi-Level inheritance in Hibernate

Currently I have a structure like this:

A
|
+--B
|
+--C

It's mapped with one table per subclass using joined tables. For historic reasons I also use a discriminator, so the current situation is as described in Section 9.1.3 of the Hibernate manual.

Question: How do I extend the mapping for a structure like this:

A
|
+--B
|  |
|  D
|
+--C

Can I <subclass> a <subclass> in the hibernate mapping? What <key>s do I need?

like image 562
Henning Avatar asked Nov 04 '08 10:11

Henning


People also ask

What are the inheritance mapping strategies in hibernate?

Hibernate supports the three basic inheritance mapping strategies: table per class hierarchy. table per subclass. table per concrete class.

Why do we use inheritance mapping in hibernate?

Entity inheritance means that we can use polymorphic queries for retrieving all the subclass entities when querying for a superclass. Since Hibernate is a JPA implementation, it contains all of the above as well as a few Hibernate-specific features related to inheritance.

Which inheritance strategy is better in hibernate?

Single Table. The single table strategy maps all entities of the inheritance structure to the same database table. This approach makes polymorphic queries very efficient and provides the best performance.


1 Answers

not tested but, according to the link you posted if you are using hibernate3

<hibernate-mapping>
  <class name="A" table="A">
    <id name="id" type="long" column="a_id">
      <generator class="native"/>
    </id>
    <discriminator column="discriminator_col" type="string"/>
    <property name="" type=""/>
    <!-- ... -->
  </class>
  <subclass name="B" extends="A" discriminator-value="B">
    <!-- ... -->
  </subclass>
  <subclass name="D" extends="B" discriminator-value="D">
    <!-- ... -->
  </subclass>
  <subclass name="C" extends="A" discriminator-value="C">
    <!-- ... -->
  </subclass>
</hibernate-mapping>
like image 152
shyam Avatar answered Oct 14 '22 07:10

shyam