7

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?

Henning
  • 11,496
  • 5
  • 27
  • 25

2 Answers2

6

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>
shyam
  • 9,134
  • 4
  • 29
  • 44
0

Using Annotations, it can be done as follows:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="LoanType",discriminatorType="String")
@Table(name = "A")
public class A implements Serializable{
}

@Entity
@Table(name= "B")
@PrimaryKeyJoinColumn(name = "B_ID", referencedColumnName ="A_ID")
public class B extends A{
}


@Entity
@Table(name= "C")
@PrimaryKeyJoinColumn(name = "C_ID", referencedColumnName = "B_ID")
public class C extends B{}
KayV
  • 12,987
  • 11
  • 98
  • 148