-->
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: IList.Remove calls SQL UPDATE?
PostPosted: Mon Jul 04, 2005 7:29 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
I have a collection mapped as a bag:

Code:
<bag name="answers" cascade="all-delete-orphan" lazy="true" access="field">
   <key column="MemberSurveyID" />
   <one-to-many class="MemberSurveyAnswer" />
</bag>


There is a business method within the MemberSurvey class that removes an element from the collection:

Code:
public void RemoveResponse(Question q)
{
   Array existingAnswers = Array.CreateInstance(typeof(MemberSurveyAnswer), this.answers.Count);

   this.answers.CopyTo(existingAnswers, 0);

   foreach (MemberSurveyAnswer answer in existingAnswers)
      if (answer.Question.Equals(q))
         this.answers.Remove(answer);
}


Using log4net, I can see that elements are in fact being deleted from the collection. However, when the session is flushed (or when I call update on the MemberSurvey object) NH is executing an UPDATE statement instead of a DELETE:

Quote:
Preparing UPDATE MemberSurveyAnswer SET MemberSurveyID = null WHERE MemberSurveyID = @p0 AND MemberSurveyAnswerID = @p1


I may not be understanding the cascade property but I thought "all-delete-orphan" would immediately cascade operations on the collection without calling session.flush for one, and the second question is, why does a call to IList.Remove() translate into an UPDATE call on the DB. Shouldn't that be a DELETE? I think I'm doing something wrong...

comments are appreciated.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 2:03 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Does it try to execute a DELETE afterwards? It may just be the way NHibernate works. Since you deleted the object from the collection, NHibernate just removes the association, but does not remove the object itself. Then the cascade should come into play and remove the entire object.

The reason is you might want to move a question to another survey and NHibernate should be prepared to handle that (even though this usage is actually quite rare in real applications).

You can try removing the question and calling Delete on it manually afterwards.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 5:09 pm 
thanks for the clue on the internals of NH. that helps in my understanding. in this instance, setting inverse="true" on the bag worked. I guess i'm still not completely understanding the inverse="true" setting...

-d


Top
  
 
 Post subject:
PostPosted: Tue Jul 12, 2005 2:24 pm 
Regular
Regular

Joined: Mon May 16, 2005 2:15 pm
Posts: 59
Anonymous wrote:
thanks for the clue on the internals of NH. that helps in my understanding. in this instance, setting inverse="true" on the bag worked. I guess i'm still not completely understanding the inverse="true" setting...

-d


I think inverse="true" basically means that the objects in the collection only exist as part of the parent.

For example, and Invoice has InvoiceLines. You can't have invoice lines without an Invoice. So, InvoiceLines would be inverse=true. So, when the Invoice is deleted, so are the LineItems.

But, think of Tennent / Unit... The tennent lives in a unit. But, if the tennent moves out the relation ship is broken, but there is still a Unit. The Tennet either moves to a different Unit or he goes away. You wouldn't want to delete the Unit just cause you delete the Tennent.

Another way perhaps to think about it is, if the FK in the child table of the relation ship field allows nulls, the inverse=false. If the FK in the child table is required then you have inverse=true. Of course, there are probably exceptions to this rule.

At least, that is my small minds understanding of them.

BOb


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.