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.  [ 10 posts ] 
Author Message
 Post subject: need help deleting mapped persistent collection
PostPosted: Wed Nov 08, 2006 1:26 pm 
Newbie

Joined: Wed Nov 08, 2006 1:05 pm
Posts: 6
Hello,

I have a one-to-many association between 2 objects:
Project ---> Baselines

in my project entity mapping, i have :

<bag name="bpbbaselines" inverse="true" cascade="all">
<key column="bpb_projects_t_id" />
<one-to-many class="com.ppc.paasmetrics.bo.bpbbaseline, com.ppc.paasmetrics" />
</bag>

in my baseline entity mapping i have

<id name="Id" type="Int32" column="bpb_baselines_t_id" unsaved-value="0">
<generator class="identity" />
</id>
<many-to-one name="bpbproject" column="bpb_projects_t_id" class="com.ppc.paasmetrics.bo.bpbproject, com.ppc.paasmetrics" />

I want to be able to delete the baselines collection and replace it with a totally new one, but for some reasons, every hting i tried did not work, and it keed adding the record in the DB, instead of deleting + adding new ones.

What I tried:

openSession();
_tx = _session.BeginTransaction();
project = (bpbproject) _session.Get(typeof(bpbproject), project.Id);
project.bpbbaselines.Clear();
project.bpbbaselines = null;
project.bpbbaselines = newbaselines;
_session.SaveOrUpdate(project);
_tx.Commit();

What am i doing wrong??

Thanks for your help,

Fabien


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 2:01 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Try flushing the session immediately after the Clear() and before you re-assign the collection reference...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 3:15 pm 
Newbie

Joined: Wed Nov 08, 2006 1:05 pm
Posts: 6
grennis wrote:
Try flushing the session immediately after the Clear() and before you re-assign the collection reference...


Tried that...not better...still add the records at the end as if the unreferenced collection was not doing anything...

Other ideas??
Thanks,

Fabien


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 4:34 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
I think the collection is lazy loaded. Try calling NHibernateUtil.Initialize(project.bpbbaselines) before you call clear


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 5:29 pm 
Newbie

Joined: Wed Nov 08, 2006 1:05 pm
Posts: 6
grennis wrote:
I think the collection is lazy loaded. Try calling NHibernateUtil.Initialize(project.bpbbaselines) before you call clear


My collections don't use lazy-loading. But I still tried your suggestion...without success.

Is it somehting to do with the fact that the baselines table use identity key (MS SQL)?

I really don't know what's going on...

Thanks,

Fabien


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 9:57 pm 
Regular
Regular

Joined: Mon Mar 20, 2006 10:49 pm
Posts: 59
Try using cascade="all-delete-orphan". Your current mapping allows baselines without parents.

_________________
Mike Abraham


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 09, 2006 3:19 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
And don't call Clear, just replace the collection with a new empty one.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 09, 2006 11:24 am 
Newbie

Joined: Wed Nov 08, 2006 1:05 pm
Posts: 6
mabraham wrote:
Try using cascade="all-delete-orphan". Your current mapping allows baselines without parents.


No success. I get:
NHibernate.HibernateException: You may not dereference an collection with cascade="all-delete-orphan"

sergey wrote:
And don't call Clear, just replace the collection with a new empty one.


I removed the clear and put new ArrayList(); instead but no more success...

Do you have a working sample that works for you?? Maybe there's something obvious that I am missing.

Thanks,

Fabien


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 8:18 am 
Newbie

Joined: Thu Aug 17, 2006 7:25 am
Posts: 7
I have found that you can get the desired behaviour by setting cascade to all-delete-orphan and calling Clear() on the collection.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 1:02 pm 
Newbie

Joined: Tue Oct 24, 2006 2:51 pm
Posts: 19
Correct solution i think has been posted in the message above
I would like to add something to explain better (i try! i'm a NH newbie as well)

1) NHibernate uses custom-collection classes even when they are not lazy, your ArrayList will be _anyway_ replaced with a class that keeps track of persistent instances

2) If you dereference the collection, the error message is obvious: you say to Nhibernate that you want to keep track of elements deletion ("all-delete-orphan") but you also destroy the collection object.. The semantics of delete managment is logically linked to the container collection, so CLEARING (0 elements) a collection is a thing, and dereferencing from the parent object has a really different meaning

In this scenario, NHibernate handled the problem correctly i think, i even noticed that the formal semantics of O/R mapping is well estabilished in NHibernate architecture, so there is no such space left for "interpretations of behiavour..."

Hope this can help,


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