-->
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: Delete/Replace the contents of a collection
PostPosted: Thu Jul 12, 2007 3:41 pm 
Newbie

Joined: Wed May 02, 2007 9:32 am
Posts: 17
Im trying to implement a "one shot delete" as described in Section 14.1.4 in
http://www.hibernate.org/hib_docs/nhibernate/html/performance.html

I have an order with a list of line items. I want to clear this list out and save a new list, but what is happening is that my new list is being appended to the old list. My code is as follows.
Code:
            CurrentOrder.LineItems = null;
            Set<Orders.Core.LineItem> newLines = new HashedSet<Orders.Core.LineItem>();
           
            foreach (ShoppingCartItem li in EditBasket.Items)
            {
                Orders.Core.LineItem add = new Orders.Core.LineItem();
                add.ParentOrder = CurrentOrder;
                add.Description = li.Description;
                add.ItemTax = li.SalesTax;
                add.QuantityOrdered = li.Quantity;
                add.ListPrice = li.ListPrice;
                add.ItemNumber = li.CatalogNumber;
                newLines.Add(add);
            }

            CurrentOrder.LineItems = newLines;
            OrdersDaoFactory.GetOrderDao().Save(CurrentOrder);


Edit: Here is the snippet of the mapping file

Code:
      <!-- bag Line Items-->
      <set name="LineItems" inverse="true" lazy="true" cascade="all">
         <key column="order_id" foreign-key="order_id" />
         <one-to-many class="LineItem"/>
      </set>


As always, any help or suggestions are appreciated.
Thanks
Joe


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 12, 2007 5:28 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
A few suggestions, don't know if any or a combination will work:

Make the changes in a single session. This may not be consistent with your DAO pattern, but I tend to think that pattern is anemic, since you lose any ability to 'converse' with the ISession, which is capable of detecting changes on an object. It might be good for your DAO methods to have versions that also take an ISession so that multiple commands can span a single session and/or transaction. If you have an absolute need for encapsulation/hiding NH, then create ISession/ITransaction wrappers.

Save your Order after dereferencing the collection. And before you add the new one. This will result in a new connection/transaction, but again, if you did it under a single session...

Initialize the collection after loading/set lazy='false'? Worth a try?

use cascade='all-delete-orphan'? Not sure if this is the behavior you intend. If it is it just might work, maybe in combination with something from the above.


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.