Problem with 2 levels of inheritance in hibernate mapping

Posted by Seth on Stack Overflow See other posts from Stack Overflow or by Seth
Published on 2010-05-19T21:50:19Z Indexed on 2010/05/20 20:20 UTC
Read the original article Hit count: 237

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!

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate