-->
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.  [ 3 posts ] 
Author Message
 Post subject: WrongClassException:Object ... not of the specified subclass
PostPosted: Tue Apr 25, 2006 12:34 pm 
Newbie

Joined: Wed Apr 19, 2006 2:51 pm
Posts: 16
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3

Mapping documents:

One Abstract Class "Step" is mapped with 2 concrete subclasses :"StepEai" and "StepSap" :

<hibernate-mapping package="bsa.business.model">
<class name="Step" abstract="true" table="step" optimistic-lock="none">
<id name="stepId" type="integer" unsaved-value="0">
<column name="step_id" not-null="true" unique="true"/>
<generator class="native"/>
</id>
<discriminator type="string" not-null="true">
<column name="descriminator" not-null="true" sql-type="VARCHAR(1)"/>
</discriminator>

... here come some properties ...

</class>
<subclass name="StepEai" extends="bsa.business.model.Step" discriminator-value="1">
<property name="script" type="string" column="script"/>
<property name="technoTrsf" type="string" column="techno_trsf"/>
</subclass>
<subclass name="StepSap" extends="bsa.business.model.Step" discriminator-value="2">
<property name="abap" type="string" column="abap" />
<property name="btci" type="string" column="btci" />
</subclass>
</hibernate-mapping>


The lists of steps (stepEaiList and stepSapList) are java.util.List properties of parent object "chaine" in a <one-to-many> relationship ; here is the mapping file of the parent Class "Chaine" :

<hibernate-mapping package="bsa.business.model">
<class name="Chaine" table="chaine" optimistic-lock="none">
<id name="chaineId" access="field" type="integer" unsaved-value="0" column="chaine_id">
<generator class="native"/>
</id>

... here some additional properties ...

<list name="stepSapList" lazy="false" cascade="none">
<key foreign-key="chaineId" column="chaine_id" update="false"/>
<list-index column="step_list_idx"/>
<one-to-many entity-name="bsa.business.model.StepSap"/>
</list>

<list name="stepEaiList" lazy="false" cascade="none">
<key foreign-key="chaineId" column="chaine_id" update="false"/>
<list-index>
<column name="step_list_idx" />
</list-index>
<one-to-many entity-name="bsa.business.model.StepEai"/>
</list>
</class>
</hibernate-mapping>




Full stack trace of any exception that occurs:
Caused by: org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: bsa.business.model.StepSap (loaded object was of wrong class)
at org.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:1235)
at org.hibernate.loader.Loader.getRow(Loader.java:1186)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:569)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1919)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:71)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1693)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:454)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:827)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:229)
at org.hibernate.loader.Loader.doList(Loader.java:2150)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:298)
at bsa.integration.service.BeanManager.findPoolList(BeanManager.java:138)
at bsa.view.jsfBeans.ActionBean.listDmex(ActionBean.java:150)
at bsa.view.jsfBeans.ActionBean.login(ActionBean.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
... 32 more


Name and version of the database you are using:MySQL 4.1.9



Both StepEai and StepSap are mapped to the same MySQL table : step.

They both use the same Primary Key field (auto-increment) : step_id ;
They both use the same Index field : step_list_idx ;

When we remove one of the StepList properties (either stepEaiList or stepSapList) from the mapping files, Hibernate instantiates the other one correctly !o!

We can't manage to have hibernate to instantiate both stepLists at the same time from the table 'step' :-(

According to the information maintained in the mapping files I understood that hibernate should be able to "descrimine" which List to instantiate with which subclass ;

Am I wrong somewhere in my analysis ? Big thanks and a lot of credits for the one who can help !!!

_________________
Cheers,
Laurent

Thanks for rating in case it helped !


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 5:48 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
There are various ways to implement this sort of thing, and in my experience, the easiest one is to not tell hibernate anythng about the java class relationships.

Get rid of Step's mapping, and map StepEai and StepSap as two unrelated mappings (you can keep the java inheritances). In the definitions for the two Step??? classes, use where="descriminator=X". Map the descriminator column as normal, but have the setDescriminator methods do nothing: the discriminator value will be constant. You need to map the descriminator column so that new entities get the correct value in that column. You could use sql-insert and hardcode the value, but I think that's over the top.

A comment about your mapping: I recommend using the subclass element as a sub-element of the class element, rather than as a sub-element of hibernate-mapping. Either works, but nesting subclass in class is less ambiguous. I don't like that hibernate gives you two ways to do the same basic thing: smacks of perl (ick).

code tags are your friend. Use them and love them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 5:12 am 
Newbie

Joined: Wed Apr 19, 2006 2:51 pm
Posts: 16
BIG THANKS !!!!

It works fine now ...

_________________
Cheers,
Laurent

Thanks for rating in case it helped !


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.