Hi, I'm not a very experienced hibernate user, such that I've actually never had to store inherited classes before, but now it is time. Based on the scant available documentation, I have the following, implemented as a table-per-class hierarchy but it is throwing exceptions:
Java class structure (abbreviated; a potentially complicating factor is that the classes in question are static inner classes):
Code:
public class Comment
{
....
public abstract static class Annotation
{
private long id;
private String text;
....
}
public static class ArrowAnnotation extends Comment.Annotation
{
}
public static class BoxAnnotation extends Comment.Annotation
{
private String field2;
....
}
//so Comment is the parent class that has a set of child Annotation objects that are defined within its scope
//at run time this is populated by entities of both of Annotation's subclasses
private Set<Annotation> annotations = new HashSet<Annotation>();
....
}
Comment.hbm.xml contains (setting aside the problems of unidirectional one-to-many associations for now):
Code:
<set cascade="all,delete-orphan" name="annotations">
<cache usage="read-write"/>
<key column="COMMENT_ID" />
<one-to-many class="com.ct.objects.Comment$Annotation" />
</set>
Annotation.hbm.xml has:
Code:
<class name="com.ct.objects.Comment$Annotation" table="ANNOTATION" abstract="true">
<id name="id" type="long" column="ANNOTATION_ID">
<generator class="native"/>
</id>
<discriminator column="type" type="string" force="true"/>
<property name="text" type="string"/>
...
<subclass name="com.ct.objects.Comment$Box" discriminator-value="box">
<property name="field2" type="string"/>
</subclass>
<subclass name="com.ct.objects.Comment$Arrow" discriminator-value="arrow">
</subclass>
</class>
The problem occurs when I try to save a Comment object whose annotations set is not empty (and by cascading, members of annotation are being saved). The error is:
Code:
org.hibernate.HibernateException: instance not of expected entity type: com.ct.objects.Comment$ArrowAnnotation is not a: com.ct.objects.Comment$Annotation
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassEntityPersister(AbstractEntityPersister.java:3583)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1347)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:487)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:84)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
It is as if hibernate is completely oblivious to the polymorphism that is happening and the presence of discriminators.
Any idea of what I am doing wrong (I really don't think the inner static nature of the classes involved makes a difference)
I am using Hibernate 3.2.1.ga