Single Table Per Class Hierarchy with an abstract superclass using Hibernate Annotations
Posted
by Andy Hull
on Stack Overflow
See other posts from Stack Overflow
or by Andy Hull
Published on 2010-03-12T02:21:08Z
Indexed on
2010/03/12
2:27 UTC
Read the original article
Hit count: 1105
I have a simple class hierarchy, similar to the following:
@Entity
@Table(name="animal")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="animal_type", discriminatorType=DiscriminatorType.STRING)
public abstract class Animal { }
@Entity
@DiscriminatorValue("cat")
public class Cat extends Animal { }
@Entity
@DiscriminatorValue("dog")
public class Dog extends Animal { }
When I query "from Animal" I get this exception:
"org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: Animal"
If I make Animal concrete, and add a dummy discriminator... such as @DiscriminatorValue("animal")... my query returns my cats and dogs as instances of Animals. I remember this being trivial with HBM based mappings but I think I'm missing something when using annotations.
Can anyone help? Thanks!
© Stack Overflow or respective owner