Single Table Per Class Hierarchy with an abstract superclass using Hibernate Annotations
- by Andy Hull
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!