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: NHibernate, inheritance and collections
PostPosted: Fri Feb 06, 2009 4:38 am 
Newbie

Joined: Fri Feb 06, 2009 4:09 am
Posts: 1
Hello!

I encountered an issue with collection (bag) mappings in parent class, i.e:

Code:
<class name="Parent" table="Parent">

   <id name="ID" column="ID" unsaved-value="0">
       <generator class="identity" />
   </id>

   <bag name="FooBag" table="FooBag" inverse="true" cascade="all">
         <key column="ParentID" />
         <one-to-many class="Foo" />
   </bag>

   <joined-subclass name="SubClass" table="SubClass">
         <key column="ID" />
          <!-- ... -->
    </joined-subclass>
</class>

<class name="Foo" table="Foo">

   <id name="ID" column="ID" unsaved-value="0">
       <generator class="identity" />
   </id>

   <many-to-one name="Parent" column="ParentID" class="Parent"  not-null="true" />

   <bag name="BarBag" table="BarBag" inverse="true" cascade="all">
         <key column="FooID" />
         <one-to-many class="Bar" />
   </bag>
</class>


When I load the SubClass item and start browsing through the FooBag,
it contains as many items as there are lines associated to it in the BarBag table. I.e
Code:
[Parent]-1-----10-[FooBag]-1-----10-[BarBag]


SubClasses list actually contains 100x FooBag each having 10x BarBag
items. Correct result would've been SubClass having 10x FooBag with
each having 10x BarBag. If one cut+pastes the <bag name="FooBag" ...>
from the Parent to SubClass, the implementation works correctly.

I've tried to re-implement equals/hashcode methods, but in vain. So, is
this a known issue, or am I just doing something horribly wrong? :)

Putting the ugliness of workarouds aside, one solution to the problem
above is to cut+paste the <bag name="FooBag" ...> to every subclass
definition.
- D -


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 6:23 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
This result is probably caused by the join which returns a "duplicate" of Foo for each bar. There may be some solutions:

- try to load parent (with a constraint p.class = subclass)
- switch from bags (which can contain duplicate items) to a set

Can you post the query you use for retrieving the subclass ?

_________________
--Wolfgang


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.