views:

148

answers:

1

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!

+1  A: 

I found a solution, so I guess I'll post it here in case anyone else runs across this problem.

It turns out that the extra annotations on class C were causing the problem. When I got rid of everything but entity and left all the other classes the way they were (with the appropriate discriminator values), everything worked correctly.

@Entity
public class C extends A
...
Seth