-->
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.  [ 4 posts ] 
Author Message
 Post subject: hibernate collection mapping issue
PostPosted: Mon Mar 07, 2005 7:57 pm 
Newbie

Joined: Thu Jan 06, 2005 12:55 am
Posts: 4
I've been stuck on this one for a couple of days and it has baffled me and other hibernate users I know. The problem goes like this:

In my hibernate mapping model I have two classes Ainc and ExpNote. I've listed the mapping docs below. ExpNotes has three subclasses ExpNoteAinc, ExpNoteBill, ExpNoteSL. There is a one-to-many relationship between Ainc and ExpNotesAinc. However when I retrieve the set of aincExpNotes I get all ExpNotes when I only wanted ExpNoteAinc objects. In Ainc.hbm.xml I've mapped the set as:

<set name="aincExpNotes" cascade="save-update" lazy="true">
<key column="explainedID"/>
<one-to-many class="ExpNoteAinc"/>
</set>

Which has spedified class="ExpNoteAinc", but it seems that the set is still not being limited to the subclass. I've read Hibernate in Action and this seems to be the suggested way to do this. Can you see anything wrong with my mapping that is causing all ExpNote objects (the parent class) to be returned instead of just the subclass ExpNoteAinc objects.


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="com.oqpc.web.valueobjects">
<class
name="ExpNote"
table="expnote"
discriminator-value="null"
lazy="true"
polymorphism="explicit"
>
<id
name="expNoteID"
column="expNoteID"
type="long"
>
<generator class="identity"/>
</id>

<discriminator column="legType" type="string"/>


<property
name="explainedID"
column="explainedID"
type="long"

/>
<property
name="numPages"
column="numPages"
type="integer"
not-null="false"
/>
<many-to-one name="file"
class="File"
column="fileID"
cascade="all"
unique="true"
/>

<subclass
name="ExpNoteBill"
discriminator-value="bill"
lazy="true"
>
<set name="addendums" cascade="save-update" inverse="true" lazy="true">
<key column="expNoteID"/>
<one-to-many class="Addendum"/>
</set>
</subclass>

<subclass
name="ExpNoteSL"
discriminator-value="sl"
lazy="true"
>
</subclass>

<subclass
name="ExpNoteAinc"
discriminator-value="ainc"
lazy="true"
>
</subclass>

</class>

</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="com.oqpc.web.valueobjects">
<class
name="Ainc"
table="ainc"
>
<id
name="aincID"
type="long"
column="aincID"
>
<generator class="identity"/>
</id>

<property
name="numPage"
column="numPage"
type="integer"
/>

<many-to-one name="bill"
class="Bill"
column="billID"
unique="true"
/>

<many-to-one name="file"
class="File"
column="fileID"
unique="true"
/>

<set name="aincExpNotes" cascade="save-update" lazy="true">
<key column="explainedID"/>
<one-to-many class="ExpNoteAinc"/>
</set>

</class>

<query name="getAincs"><![CDATA[
from Ainc ainc left join fetch ainc.file left join fetch ainc.bill left join fetch ainc.aincExpNotes expNotes left join fetch expNotes.file
]]></query>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 7:57 pm 
Newbie

Joined: Tue Mar 08, 2005 7:11 pm
Posts: 9
Location: Sydney, Australia
You can get around this by specifing a "where" attribute for your collection mapping, such that you manually limit it based on the subclass discrimintator (why doesn't Hibernate do this automatically!?!?!)

<set name="aincExpNotes" cascade="save-update" lazy="true" where="BILL='ainc'">
<key column="explainedID"/>
<one-to-many class="ExpNoteAinc"/>
</set>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 21, 2006 11:59 am 
Regular
Regular

Joined: Tue Sep 28, 2004 5:18 pm
Posts: 55
Location: Switzerland
So how would this be done with annotations?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 21, 2006 12:15 pm 
Regular
Regular

Joined: Tue Sep 28, 2004 5:18 pm
Posts: 55
Location: Switzerland
@Where(clause="discriminator_column_name='discriminator_value'")


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