-->
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.  [ 5 posts ] 
Author Message
 Post subject: Parent-child problem
PostPosted: Sun Aug 03, 2008 2:04 am 
Newbie

Joined: Mon Jul 14, 2008 7:08 am
Posts: 18
Standart problem situation:
class Parent { ... IList<Child> childList; ... }
And
class Child { ... Parent parent; ...}
Parent contains list of childs. I can manipulate childs lifecycle using "cascade" attribute in parent mapping definition. Using this I can, for example, delete child just by removing it from childList list.
But what if I want to remove child in situation where I have no reference to its parent and therefore to parent's childList? Session.Remove(childInstance) don't work. It works only if I delete childInstance from childList manually.

Very often I need to iterate through childLists of many parents (they are all detached) and I would like to delete some childs from these lists during iteration. But this is not possible because lists cannot be changes during iteration. So I have to store appropriate parents and childs to perform removing from childLists after iteration. It would be cool if everything I need to do is to write something like this: childInstance.Parent = null and then reattach parents and their childs to session and write session.delete(childInstance). Is that possible?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 8:16 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Should be ... what do you mean with
Quote:
Session.Remove(childInstance) don't work
? Do you get an exception or is the object simply still there ?

Can you post the mappings and the code where you delete the child (code between session.BeginTransaction and session.Commit() or between session obening and flushing) ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 8:50 am 
Newbie

Joined: Mon Jul 14, 2008 7:08 am
Posts: 18
wolli wrote:
Do you get an exception or is the object simply still there ?

Objects are still there - nothing happens, no exeptions. I read that I should delete object itself and its reference in parent collection...

wolli wrote:
Can you post the mappings and the code where you delete the child (code between session.BeginTransaction and session.Commit() or between session obening and flushing) ?


Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Parent" table="Parent">
      <id ...</id>
      ...
      <bag name="Childs" lazy="false" inverse="true" cascade="all">
         <key>
            <column name="ParentId" not-null="true" />
         </key>
         <one-to-many class="Child" />
      </bag>   
   </class>
</hibernate-mapping>


Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Child" table="Child">
      <id>...</id>
                ....
      <many-to-one name="Parent" class="Parent" column="ParentId" not-null="true"/>
      
   </class>


</hibernate-mapping>


I delete the child on offline client just adding it to the collection. Then I pass this collection on server and call Session.Delete for each element in this collection. Ids are guids, so I guess hnibernate can attach childs by guid-id and then just to delete it. But nothing happens. It only works when on the same transaction I update attached parents with deleted childs in their child collections.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 9:27 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I suppose you've tried some of these already, but to rule them out:

- use a uni-directional mapping instead of the bi-directional, e.g. no parent on the child
- set the parent to null before delete.

Have you checked the debug log. Is there anything about the deletion request ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 11:02 am 
Newbie

Joined: Mon Jul 14, 2008 7:08 am
Posts: 18
wolli wrote:
use a uni-directional mapping instead of the bi-directional, e.g. no parent on the child

I'm afraid I won't be able to reference parent from child instance in business code. Or I'm mistaken?

wolli wrote:
set the parent to null before delete

I tried bit didn't help

wolli wrote:
Have you checked the debug log. Is there anything about the deletion request ?

no deletion there (((


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