-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Implementing polymorphic associations - "unexpected entity"?
PostPosted: Thu Jun 23, 2011 7:19 pm 
Newbie

Joined: Thu Jun 23, 2011 6:59 pm
Posts: 2
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


Top
 Profile  
 
 Post subject: Re: Implementing polymorphic associations - "unexpected entity"?
PostPosted: Thu Jun 23, 2011 8:02 pm 
Newbie

Joined: Thu Jun 23, 2011 6:59 pm
Posts: 2
Nevermind, after having upgraded hibernate to 3.6, it gave me a slightly differently worded error that clued me into the fact that I had misnamed the classes in the .xml. Since I can't delete the post now, please ignore it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.