-->
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.  [ 10 posts ] 
Author Message
 Post subject: Difference between set, map, bag and list
PostPosted: Mon Apr 03, 2006 5:17 pm 
Newbie

Joined: Mon Apr 03, 2006 5:12 pm
Posts: 15
Location: Glasgow, Scotland
I am mapping a one-to-many relationship between two tables. I have seen many examples of this with the collection in the parent object mapped using set, map, bag or list.

Can anyone describe the differences in using these?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 7:41 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
A set is a collection in which no item occurs more than once. This is the most common persistent collection type, in my experience.

A bag is a colleciton in which items may occur more than once: they are very inefficient because hibernate cannot tell if items you put into it are the same as ones already in it (assuming that they are equal), so it has to delete the entire collection and re-save it from memory.

A list is an indexed bag. The index lets hibernate know whether or not a particular in-memory object is the same one as an equal on-DB object, so complete delete/re-insert is not needed.

A map is just like a list, except the index doesn't have to be a computable (usually sequential) integer, it can be anything, even another object.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 2:18 am 
Newbie

Joined: Mon Apr 03, 2006 5:12 pm
Posts: 15
Location: Glasgow, Scotland
Perfect. Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 3:41 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
is this answer also applicable to nHibernate?
I'm using bags more then sets and I'm not seeing complete deletion and resaves occuring.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 5:43 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It does apply to NHibernate too, yes. Bags don't need complete deletion when you're modifying existing members, but when you add or remove an item from the bag, the entire collection should be deleted/reinserted. If you're not seeing this behaviour, you should write a test to verify that you're seeing the expected results after these operations. If you are, I wouldn't worry about it: perhaps there's special cases when hibernate can avoid complete collection deleteion operations. I know that they're not needed for idbags, perhaps there are similar situations where they're not needed.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 10:46 pm 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
thnx


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 10, 2008 5:34 am 
Newbie

Joined: Tue Jul 08, 2008 8:02 am
Posts: 10
tenwit wrote:
A bag is a colleciton in which items may occur more than once: they are very inefficient because hibernate cannot tell if items you put into it are the same as ones already in it (assuming that they are equal), so it has to delete the entire collection and re-save it from memory.


From your post it seems that bag does not have any advantage. But it surely has, as otherwise it would not be there. Can you tell us?

Thanks,
Viliam


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 7:17 am 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
tenwit wrote:
A bag is a colleciton in which items may occur more than once: they are very inefficient because hibernate cannot tell if items you put into it are the same as ones already in it


I'm happy to learn, but as far as I can see you have it exactly the wrong way around.

A set is very inefficient if/when adding to a large collection, because NHibernate has to load the entire collection into memory in order to ensure that uniqueness is preserved.

A bag suffers no such thing. Why would it care? - it's allowed to have duplicates.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 9:21 am 
Newbie

Joined: Tue Jul 08, 2008 8:02 am
Posts: 10
Set is inefficient with large collections, but bag is as well: it has to delete whole large collection and re-insert it to add/remove one single row. It seems that list is best at performance while set enforces uniqueness at the cost of performance, if you need it.

And again: where is bag more appropriate than these two?

Viliam


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 12, 2008 6:00 am 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
viliam.durina wrote:
Set is inefficient with large collections, but bag is as well: it has to delete whole large collection and re-insert it to add/remove one single row.


Hey Viliam,

What makes you think that? I've used a lot of bags in my time, and none of them have done that.

Unless you're thinking of the 'all-delete-orphan' mapping?


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