-->
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: Wrong Class exception while doing lazy loading
PostPosted: Wed Jul 13, 2005 2:22 pm 
Newbie

Joined: Wed Jul 13, 2005 12:40 pm
Posts: 2
I have 3 Entities that I am dealing with:
Employee, Dependent and Beneficiary. All of them extend the abstract type person,
I am using a table per hierarchy format, hence all the of the entities are in the same table.
Employee has one-to-many relation with Beneficiary and Dependent.
everything works fine when I add or get dependents from or to the employee. However when I do employee.getBeneficiaries,
where beneficiaries is a lazily loaded Set which gets objects of type Beneficiary from the database, I get the exception below..

Please refer to the mapping files ..

Mapping file for Person


<hibernate-mapping
>
<class
name="com.tps.gen.bo.Person"
table="xperson"
>
<cache usage="transactional" />

<id
name="guid"
column="guid"
type="java.lang.String"
length="36"
>
<generator class="com.tps.framework.core.GuidGenerator">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Person.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<discriminator
column="classtype"
force="true"
/>

<version
name="version"
column="version"
type="java.lang.Integer"
unsaved-value="negative"
/>


......

<subclass
name="com.tps.gen.bo.Beneficiary"
>

<component
name="allotment"
class="com.tps.type.Percent"
>

<property
name="value"
type="java.lang.Double"
update="true"
insert="true"
column="allotment_value"
/>

</component>

<many-to-one
name="employee"
class="com.tps.gen.bo.Employee"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
column="employee_guid"
not-null="false"
/>

.....

</subclass>
<subclass
name="com.tps.gen.bo.Dependent"
>

</component>

<many-to-one
name="employee"
class="com.tps.gen.bo.Employee"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
column="employee_guid"
not-null="false"
/>

.....


<subclass
name="com.tps.gen.bo.Employee"
>


<set
name="dependents"
lazy="true"
cascade="all"
sort="unsorted"
>

<key
column="employee_guid"
>
</key>

<one-to-many
class="com.tps.gen.bo.Dependent"
/>

</set>


<set
name="beneficiaries"
lazy="true"
cascade="all"
sort="unsorted"
>

<key
column="employee_guid"
>
</key>

<one-to-many
class="com.tps.gen.bo.Beneficiary"
/>

</set>


Hibernate Version: 2.1
Database: MySQL 4.1


I am facing a WrongClassException:

11:00:13,718 ERROR [net.sf.hibernate.collection.PersistentCollection] - Failed to lazily initialize a collection
net.sf.hibernate.WrongClassException: Object with id: cff4e90f-5599-4f87-8e7f-84adab68856f was not of the specified subclass: com.tps.gen.bo.Beneficiary (loaded object was of wrong class)
at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:535)
at net.sf.hibernate.loader.Loader.getRow(Loader.java:502)
at net.sf.hibernate.loader.Loader.getRowFromResultSet(Loader.java:218)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:285)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:1020)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:995)
at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:93)
at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:288)
at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3315)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:195)
at net.sf.hibernate.collection.PersistentCollection.write(PersistentCollection.java:84)
at net.sf.hibernate.collection.Set.clear(Set.java:212)
at net.sf.hibernate.type.PersistentCollectionType.copy(PersistentCollectionType.java:235)
at net.sf.hibernate.type.TypeFactory.copy(TypeFactory.java:284)
at net.sf.hibernate.impl.SessionImpl.doCopy(SessionImpl.java:4113)
at net.sf.hibernate.impl.SessionImpl.copy(SessionImpl.java:4045)
at net.sf.hibernate.engine.Cascades$5.cascade(Cascades.java:132)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.impl.SessionImpl.doCopy(SessionImpl.java:4107)
at net.sf.hibernate.impl.SessionImpl.copy(SessionImpl.java:4045)
at net.sf.hibernate.engine.Cascades$5.cascade(Cascades.java:132)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.impl.SessionImpl.doCopy(SessionImpl.java:4107)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdateCopy(SessionImpl.java:4041)

I am sure that there are no objects of type beneficiary in the database. I traced the error, and it seems like it is loading an object of type dependent in the set of beneficiaries, which is causing the error.

Does anyone have an idea of how to rectify this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 4:22 pm 
Newbie

Joined: Tue May 11, 2004 11:32 am
Posts: 16
I've had the same problem. I solved it by adding a where attribute to the set that checks the discriminator column is equal to the discrimnitor-value for the subtype you want. I don't see discrimintor-value in your mapping so you probably need to add that.

Code:
<set
  name="beneficiaries"
  lazy="true"
  cascade="all"
  sort="unsorted"
  where="classtype='BENEFICIARY'"
>


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 6:55 pm 
Beginner
Beginner

Joined: Wed Sep 28, 2005 5:30 pm
Posts: 25
Perman,

Any idea why this "where" attribute is needed? Hibernate should already know what the discriminator value is by looking up the specified "class" and then using "discriminator". Any idea why it doesn't do this?


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.