tags:

views:

773

answers:

3

I am looking for what most people use as their collection type when making one-to-many associations in Hibernate. The legacy application I am maintaining uses bags exclusively, but keeps them as lists in code. The tables associated have an id field, so an idbag seems more appropriate, but documentation recommends a Set.

EDIT: I mistakenly referenced that the documentation recommends a set. In reality, the official documentation is equally vague on all collection types. What I find is that some websites seem to infer that Set is the most common, and the Hibernate book I am reading explicitly says this about sets:

This is the most common persistent collection in a typical Hibernate application. (see: page 242 of 'Java Persistence with Hibernate' by Christian Bauer and Gavin King)

I guess that is what threw me and made me seek out what others are using.

+1  A: 

I'm guessing people use all kinds of things :-) - different collection types serve different purposes so the "best" one depends on what you need it for.

That said, using List in code is usually more convenient than using Set even though said List is unordered. If nothing else, '.get(0)' is easier on the eyes than .iterator().next() :-) Hibernate bag support is definitely adequate for this purpose plus you can even add an order-by declaration (if applicable) and have your list sorted.

idbag is a whole different animal used for many-to-many associations; you can't really compare it to regular Set or List.

ChssPly76
ChssPly76 said: "using List in code is usually more convenient than using Set even though said List is unordered. If nothing else, '.get(0)' is easier on the eyes than .iterator().next()" - I agree with this, which is what threw me when all the documentation recommends Sets. I just want to see what other people are using and if it goes against the documentation. Thanks.
Nemi
You keep saying "documentation recommends Sets". Can you provide a specific link? I don't think Hibernate documentation recommends one collection type over another (how could it?); it just describes various available mappings. There isn't a single "recommend" word in the chapter on collections: http://docs.jboss.org/hibernate/stable/core/reference/en/html/collections.html
ChssPly76
@ChssPly76 - you are correct of course. Please see my edit above.
Nemi
A: 

I would recommend using a set because a set is defined as a collection of unique items and thats normally what you deal with.

And .iterator().next() is save when there is no element in your collection.

.get(0) might throw an IndexOutOfBoundsException if you access an empty list.

MrWhite
Riiiiight... Care to try it? `iterator().hasNext()` is safe; `iterator().next()` is most certainly not.
ChssPly76
Of course you would check that by using the iterators hasNext method, but you never know on which position an element is in a list.
MrWhite
You may just as well check whether list is empty (via `isEmpty()`, which is - again - more readable). I'm not sure what you mean by "which position" - seeing as how you're comparing List to Set, your List is unordered anyway.
ChssPly76
At the end it is a matter of taste. But normally you deal with unique unordered items which a set is designed for.
MrWhite
A: 

Ok, after quite some time I have found a reason NOT to use a Set as a collection type. Due to problems with the hashcode/equals overrides and the way hibernate persists, using any java API functionality that calls hashcode/equals is a bad idea. There is no good way to consistently compare objects pre- and post-persistence. Stick with collections that do not rely on equals/hashcode like bag.

More info here:

http://community.jboss.org/wiki/EqualsandHashCode (this link makes it sound like a business key is the way to go, but read the next link fully to see why that is not always a good idea)

https://forum.hibernate.org/viewtopic.php?f=1&t=928172 (read the whole discussion to make your head spin)

Nemi