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: Can't add more than one unsaved item to an EntityList<>
PostPosted: Mon Nov 06, 2006 5:12 pm 
Newbie

Joined: Mon Oct 30, 2006 6:19 pm
Posts: 3
I've read and re-read the nhibernate and generics documentation and am at a total loss for why this is happening here...

Baiscally I retrieve an object using nhibernate and store it in the http session. The object has a lazy bag collection. On subsequent post backs, the object is retrieved from the session and a new (unsaved) item is added to the collection.

The problem is this: On the first post-back, the item is added and shows up in the collection. On subsequent post-backs, items do not appear in the collection after calling Add(item) on the EntityList.

A note: I've read the generics "gotcha" and I'm ensuring that the collection is initialized as soon as the initial object is loaded. I cannot save the items being added to the collection at the time they are added so they need to be transient in the collection until I persist the original object.

When the original object is saved using update() the one unsaved item in the collection gets saved no problem.

If I DO save the item being added to the collection after it has been added then subsequent items that are added DO show up fine. So basically it comes down to the fact that it seems as though I can only have one unsaved item in an EntityList at a time.

Can anyone explain this behvaior? Are there any work-arounds?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 6:38 pm 
Newbie

Joined: Mon Oct 30, 2006 6:19 pm
Posts: 3
Ok so just as an addendum:

When I assign a key value to the item I'm adding to the collection (which, by the way, uses an identity type key and an unsaved value of 0), subsequent items can be added to the collection provided they have a unique key.

Generally speaking, it seems like a good idea for the entityset to enforce unique key contraints - except when the items are unsaved. So I guess my original question still remains... is there a workaround to this? Is it possible to add multiple unsaved items to an entitylist?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 7:30 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Your problem is definitely with your override of Equals() method. Make sure you test ALL fields for equality.

Don't forget to mark this post helpful, since this definitely going to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 8:32 pm 
Newbie

Joined: Mon Oct 30, 2006 6:19 pm
Posts: 3
grennis wrote:
Your problem is definitely with your override of Equals() method. Make sure you test ALL fields for equality.

Don't forget to mark this post helpful, since this definitely going to solve your problem.


Indeed it did. Thanks a lot for the reply - my equals override wasn't thorough enough.

Cheers,
Derek


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 9:36 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Thanks!


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.