Hello, there Ranch folks! I’m facing a problem with multi-level inheritance in Hibernate, mixing inheritance strategies. I have not found any examples or solutions to this particular problem on the web, so let me try to describe it:
This is my class hierarchy:
A (abstract class)
|-B (abstract class)
| |-C
| |-D
|-E
In other words: C and D extend B while E and B extend A. Classes B and A are abstract.
My database is set this way: I have a table named “table_A”, a table named “table_B” and a table named “table_E”.
I’m using Java 5 with Hibernate 3.2.1 (with Annotations 3.2.0). The mapping looks like this:
code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name=“ID_A”)
@Table(name=“table_A”)
public abstract class A extends MappedEntity {…}
@Entity
@Table(name = “table_E”)
@PrimaryKeyJoinColumn(name = “ID_E”)public class E extends A {…}
@Entity
@Table(name = “table_B”)
@PrimaryKeyJoinColumn(name = “ID_B”)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = B.DISCRIMINATOR_FIELD, discriminatorType = DiscriminatorType.INTEGER)public abstract class B extends A {…}
@Entity
@DiscriminatorValue(“C”)public class C extends B {…}
@Entity
@DiscriminatorValue(“D”)public class D extends B {…}
The real problem happens when I’m trying to query for a list of entities of type A. Looks like Hibernate is not trying to instantiate C and D by the single table strategy. Hibernate is trying to resolve C and D using the InheritanceType.JOINED strategy, looking for tables with names “C” and “D”, since the name of the table is not specified. Apparently Hibernate is ignoring the second strategy (InheritanceType.SINGLE_TABLE). I read somewhere (hard to find out where, after so many searches) that Hibernate can handle mixing strategies in this multi-level inheritance problem. What am I doing wrong?