Problem with 2 levels of inheritance in hibernate mapping
- by Seth
Here's my class structure:
class A
class B extends A
class C extends A
class D extends C
class E extends C
And here are my mappings (class bodies omitted for brevity):
Class A:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@MappedSuperclass
@DiscriminatorColumn(
name="className",
discriminatorType=DiscriminatorType.STRING
)
@ForceDiscriminator
public abstract class A
Class B:
@Entity
@DiscriminatorValue("B")
public class B extends A
Class C:
@Entity
@DiscriminatorValue("C")
@MappedSuperclass
@DiscriminatorColumn(
name="cType",
discriminatorType=DiscriminatorType.STRING
)
@ForceDiscriminator
public abstract class C extends A
Class D:
@Entity
@DiscriminatorValue("D")
public class D extends C
Class E:
@Entity
@DiscriminatorValue("E")
public class E extends C
I've got a class F that contains a set of A:
@Entity
public class F
{
...
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinTable(
name="F_A",
joinColumns = @JoinColumn(name="A_ID"),
inverseJoinColumns = @JoinColumn(name="F_ID")
)
private Set<A> aSet = new HashSet<A>();
...
The problem is that whenever I add a new E instance to aSet and then call session.saveOrUpdate(fInstance), hibernate saves with "A" as the discrimiator string. When I try to access the aSet in the F instance, I get the following exception (full stacktrace ommitted for brevity):
org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: path.to.class.A
Am I mapping the classes incorrectly? How am I supposed to map multiple levels of inheritance?
Thanks for the help!