-->
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: Delete LineItem in Customer/Order/Product mapping (19.3)
PostPosted: Sat Feb 02, 2008 9:18 pm 
Newbie

Joined: Fri Feb 01, 2008 1:11 am
Posts: 3
I'm wondering what is the best way to delete a LineItem in the mapping example in section 19.3 of the documentation.

For example, if a customer wanted to remove a product from their order, I would assume you would simply say;

session.delete(order.LineItems[0])

But this does not seem to work, (LineItem is an unknown entity).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 2:46 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Since LineItem is a composite element of Order, you should simply remove the LineItem in question from the collection, then Update() the Order--or if the Order is already associated with the session, just Flush().


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 16, 2008 12:13 am 
Newbie

Joined: Fri Feb 01, 2008 1:11 am
Posts: 3
Quote:
Since LineItem is a composite element of Order, you should simply remove the LineItem in question from the collection


Its the simply part thats throwing me.

my map looks like this
Code:
<bag name="LeafList" table="Leaf_Branch">
  <key column="BranchID"/>
  <composite-element class="Leaf_Branch">
     <many-to-one name="Leaf" column="LeafID" />
  </composite-element>
</bag>


I try to remove the item like so (the commented line works as expected)
Code:
branch = session.Load<Branch>(leafBranch.BranchID);
branch.LeafList.Remove(leafBranch);
//branch.LeafList.RemoveAt(0);

The Remove() method fails. I assume the problem is that branch.LeafList doesn't have a BranchID, but leafBranch does.



Code:
public class Leaf_Branch
{

        string _branchID;
        public virtual string BranchID
        {
            get { return _branchID; }
            set { _branchID = value; }
        }

        Leaf _leaf;
        public virtual Leaf Leaf
        {
            get { return _leaf; }
            set { _leaf= value; }
        }
}

CREATE TABLE Leaf_Branch
(
   LeafID CHAR(32) NOT NULL,
   BranchID CHAR(32) NOT NULL,
)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 3:02 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Something is confused in there, mainly because I think you have oversimplified things. If your association object ("Leaf-Branch"?) has no other properties, there is no point, you should just use a many-to-many relation. I'm not sure if that is your situation, or you've just simplified away it's other properties for the sake of demonstration.

If the latter, then I wonder why the object is carrying a "BranchID" property. That shouldn't be at all. BranchID is the column in hte table that related the two, but object-wise the relation is shown by the Leaf-Branch's membership in LeafList, no more. The Remove failure might have something to do with the fact that the the Leaf-Branch that is passed to remove is a different instance (representing the same row) as the one in the LeafList. I don't know what "LeafList not having a BranchID" means; LeafList only has Leaf-Branches. Another problem: even if this does work, you might see some really bad performance: the bag might have to remove and then re-add all the list elements (see http://www.hibernate.org/hib_docs/nhibe ... ollections ).

If the former (you didn't oversimplify, and this is actually what you have) you'd be far better off using a <set> many-to-many.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 9:34 pm 
Newbie

Joined: Fri Feb 01, 2008 1:11 am
Posts: 3
Thanks for your help marcal; I really appreciate your time.

Your right, I have stripped out a lot for demonstration. (hence the bag vs list) But I don't really have any buisness rules yet (i'm still setting up the database); I've been trying to get a handle on hibernate, and have been doing a lot of expermentation.

I can see my mistake with BranchID, but I'm going around and around. What I really want to do is simply remove an item from a collection. My needs seem quite similar to the example in section 19.3 so I followed that mapping.

Going back to that example. How would one say; remove a link to a product from the order, given the product id.

if one were to say
Code:
order.LineItems.Remove(lineItem);

How do I get a hold of the lineItem instance to remove.

I can iterate through the LineItems collection and check for
Code:
foreach (LineItem item in order.LineItems)
{
  if (order.LineItems.Product.ID == id)
  {
    lineItem = item;
    break;
  }
}
order.LineItems.Remove(lineItem);



But I feel like I should be able to use the ORM to do this sort of thing, and query directly for the lineItem with a spacific product id. I'm I still missing something or is this the best way to get at the lineItem?


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.