-->
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: joined subclasses, classcastexception and determine subclass
PostPosted: Wed Jan 11, 2006 10:40 am 
Newbie

Joined: Wed Dec 07, 2005 5:01 am
Posts: 2
hi. pls need help with hibernate with joined subclasses. if i get object (PorC - is parent for person and company) directly using session.get() i can use instanceof to determine which object from joined subclasses it is (if person or company). but if i get to that object through relation e.g from address /mapped many to one/ i can get only object PorC and i am not able to determine which joined subclass it is /person or company/. if i try to change object /recast/ to person /i know that that address belongs to person/ system generate error classcastexception

any advice pls?

mapping of joined subclasses:

<hibernate-mapping>

<class name="sk.bantip.productiv.Main.Source.MVC.PCR.bin.PCR.PCRDTO" table="paPCRNew.pcr">

<id name="PCRId" column="PCRId" type="java.lang.Integer">

<generator class="native">


</generator>
</id>
<many-to-one name="source" class="sk.bantip.productiv.Main.Source.MVC.Main.bin.Source.SourceDTO" cascade="none" outer-join="auto" update="true" insert="true" column="sourceid"/>
<property name="isVisible" type="java.lang.String" update="true" insert="true" column="isVisible"/>
<many-to-one name="mutator" class="sk.bantip.productiv.Main.Source.MVC.Main.bin.User.UserDTO" cascade="none" outer-join="auto" update="true" insert="true" column="mutatorid"/>
<property name="mutatorTS" type="java.util.Date" update="true" insert="true" column="mutatorTS"/>
-
<set name="address" lazy="true" inverse="true" cascade="all-delete-orphan" sort="unsorted">
<key column="pcid">
</key>
<one-to-many class="sk.bantip.productiv.Main.Source.MVC.AC.bin.Address.AddressDTO"/>
</set>
-
<set name="contact" lazy="true" inverse="true" cascade="all-delete-orphan" sort="unsorted">
<key column="pcrid">
</key>
<one-to-many class="sk.bantip.productiv.Main.Source.MVC.AC.bin.Contact.ContactDTO"/>
</set>
-
<set name="label" lazy="false" inverse="true" cascade="all-delete-orphan" sort="unsorted">
<key column="pcrid">
</key>
<one-to-many class="sk.bantip.productiv.Main.Source.MVC.Label.bin.Label.LabelDTO"/>
</set>

<joined-subclass name="sk.bantip.productiv.Main.Source.MVC.PCR.bin.PCR.RelationDTO" table="paPCRNew.relation">
<key column="relationid"/>
<many-to-one name="person" class="sk.bantip.productiv.Main.Source.MVC.PCR.bin.PCR.PersonDTO" cascade="none" outer-join="auto" update="true" insert="true" column="personid"/>
<many-to-one name="company" class="sk.bantip.productiv.Main.Source.MVC.PCR.bin.PCR.CompanyDTO" cascade="none" outer-join="auto" update="true" insert="true" column="companyid"/>
<many-to-one name="position" class="sk.bantip.productiv.Main.Source.MVC.PCR.bin.Position.PositionDTO" cascade="none" outer-join="auto" update="true" insert="true" column="positionid"/>
<property name="isDefaultPerson" type="java.lang.String" update="true" insert="true" column="isDefaultPerson"/>
</joined-subclass>
-
<joined-subclass name="sk.bantip.productiv.Main.Source.MVC.PCR.bin.PCR.CompanyDTO" table="paPCRNew.company">
<key column="companyid"/>
<property name="name" type="java.lang.String" update="true" insert="true" column="name"/>
-
<set name="relation" lazy="true" inverse="true" cascade="all-delete-orphan" sort="unsorted">
<key column="companyid">
</key>
<one-to-many class="sk.bantip.productiv.Main.Source.MVC.PCR.bin.PCR.RelationDTO"/>
</set>
</joined-subclass>
-
<joined-subclass name="sk.bantip.productiv.Main.Source.MVC.PCR.bin.PCR.PersonDTO" table="paPCRNew.person">
<key column="personid"/>
<property name="lastName" type="java.lang.String" update="true" insert="true" column="lastName"/>
<property name="firstName" type="java.lang.String" update="true" insert="true" column="firstName"/>
<property name="van" type="java.lang.String" update="true" insert="true" column="van"/>
<property name="initials" type="java.lang.String" update="true" insert="true" column="initials"/>
<many-to-one name="titleFirst" class="sk.bantip.productiv.Main.Source.MVC.PCR.bin.TitleFirst.TitleFirstDTO" cascade="none" outer-join="auto" update="true" insert="true" column="titlefirstid"/>
<many-to-one name="titleLast" class="sk.bantip.productiv.Main.Source.MVC.PCR.bin.TitleLast.TitleLastDTO" cascade="none" outer-join="auto" update="true" insert="true" column="titlelastid"/>
-
<set name="relation" lazy="true" inverse="true" cascade="all-delete-orphan" sort="unsorted">
<key column="personid">
</key>
<one-to-many class="sk.bantip.productiv.Main.Source.MVC.PCR.bin.PCR.RelationDTO"/>
</set>
</joined-subclass>
</class>
</hibernate-mapping>



and address mapping

<hibernate-mapping>
-
<class name="sk.bantip.productiv.Main.Source.MVC.AC.bin.Address.AddressDTO" table="paac.address">

<id name="addressId" column="addressId" type="java.lang.Integer">

<generator class="native">


-->
</generator>
</id>
<many-to-one name="pcr" class="sk.bantip.productiv.Main.Source.MVC.PCR.bin.PCR.PCRDTO" cascade="none" outer-join="auto" update="true" insert="true" column="PCId"/>
<property name="isDefaultAddress" type="java.lang.String" update="true" insert="true" column="isDefaultAddress"/>
<property name="type" type="java.lang.String" update="true" insert="true" column="type"/>
<property name="street" type="java.lang.String" update="true" insert="true" column="street"/>
<property name="zip" type="java.lang.String" update="true" insert="true" column="zip"/>
<property name="city" type="java.lang.String" update="true" insert="true" column="city"/>
<property name="province" type="java.lang.String" update="true" insert="true" column="province"/>
<property name="country" type="java.lang.String" update="true" insert="true" column="country"/>
<many-to-one name="source" class="sk.bantip.productiv.Main.Source.MVC.Main.bin.Source.SourceDTO" cascade="none" outer-join="auto" update="true" insert="true" column="sourceid"/>
<many-to-one name="mutator" class="sk.bantip.productiv.Main.Source.MVC.Main.bin.User.UserDTO" cascade="none" outer-join="auto" update="true" insert="true" column="mutatorid"/>
<property name="mutatorTS" type="java.util.Date" update="true" insert="true" column="mutatorTS"/>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 1:58 pm 
Newbie

Joined: Mon Dec 12, 2005 10:45 am
Posts: 12
I also had the same problem.
This is because the associated object is proxyed by hibernate and is in reality of type "<base-class-type>$$EnhancerByCGLIB$$<some-hex-value>"

The only solution that I know to this problem is to declare the association lazy="false" so that the associated object is not proxyed, but this is not always a satifying solution.

Pascal Jacob.


Top
 Profile  
 
 Post subject: joined subclasses, classcastexception and determine subclass
PostPosted: Mon Jan 23, 2006 5:41 pm 
Newbie

Joined: Mon Jan 23, 2006 4:36 pm
Posts: 1
I too am having the same problems. On most relationships, I get back the real object but there's a select few relationships where I get back the proxy object and it causes havoc. Not only in my code by I can't merge it back into the EntityManager to save changes either (I'm using EJB 3.0).

I find it frustrating that I can't find more definitive information on what is going on and how to find an optimal solution.

From my experience there are only two sure ways to solve this: using lazy=false, or using JOIN FETCH queries, although these can introduce problems of their own.

I don't like using EAGER relationships when I don't have to however the few places where I am having trouble are on N-to-1 relationships so it shouldn't have a lot of unnecessary objects being loaded.

I find it interesting that Hibernate folks themselves suggest setting lazy=false as a solution to this when elsewhere in their documentation they strongly suggest avoiding eager relationships since they may load too many unneeded objects -- possibly the entire database in a worst case scenario.

I wish I could find a better solution but so far I can't.


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.