-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to add new items to an ISet?
PostPosted: Wed Nov 16, 2005 6:34 pm 
Regular
Regular

Joined: Sun Sep 25, 2005 11:35 pm
Posts: 57
I have a one to many relationship between Order and OrderItem. Order keeps an ISet of OrderItems. I would like to create an Order object, add bunch of OrderItems to it and then persist the entire order. Problem is that as I start adding OrderItems to my Order, the collection keeps on replacing the previous item with the new one and I end up with only one item in my collection. This is because my OrderItem class implements an Equal method which checks the IDs for equality. Of course all new items that I add have an ID of 0 and they satisfy the equality condition. Is there a solution for this?

The only workaround I could think of is to persist each item in the database (thus getting a real ID) and then adding it to the Order. But this is such a pain. I cannot create simple in-memory orders with bunch of items.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 9:28 am 
Newbie

Joined: Tue Oct 04, 2005 9:25 am
Posts: 2
You can manually create the id at object creation (works better with guids as they are guaranteed to be unique), and add a version tag to the xml file.

Therefore, it would look something like this:

Code:
public class OrderItem {
   // Fields
   private Guid m_ID = Guid.NewGuid();
   private int Version = -1;
   
   // Properties
   public Guid ID {
      get { return this.m_ID; }
   }
        ...
}


and your mapping file would have the following :
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
    <class name="..." table="OrderItem">
        <id name="ID" column="ID" type="Guid">
            <generator class="assigned"/>
        </id>
        <version name="Version" access="field" unsaved-value="-1"/>
        ...
    </class>
</hibernate-mapping>


Note: you need to add the version field to your table. You can also use a timestamp instead of the version...

So when persisting the object, nhibernate will see that you manually assigned the id and will instead verify the unsaved-value of either the version or timestamp.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 7:41 pm 
Regular
Regular

Joined: Sun Sep 25, 2005 11:35 pm
Posts: 57
Thanks. This is very helpful.


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