5

I have tried searching on Stack Overflow and at other websites the pros, cons and conveniences about using Sets vs Lists but I really couldn't find a DEFINITE answer for when to use this or that.

From Hibernate's documentation, they state that non-duplicate records should go into Sets and, from there, you should implement your hashCode() and equals() for every single entity that could be wrapped into a Set. But then it comes to the price of convenience and ease of use as there are some articles that recommend the use of business-keys as every entity's id and, from there, hashCode() and equals() could then be perfectly implemented for every situation regardless of the object's state (managed, detached, etc).

It's all fine, all fine... until I come across on lots of situations where the use of Sets are just not doable, such as Ordering (though Hibernate gives you the idea of SortedSet), convenience of collectionObj.get(index), collectionObj.remove(int location || Object obj), Android's architecture of ListView/ExpandableListView (GroupIds, ChildIds) and on... My point is: Sets are just really bad (imho) to manipulate and make it work 100%.

I am tempted to change every single collection of my project to List as they work very well. The IDs for all my entities are generated through MYSQL's auto-generated sequence (@GeneratedValue(strategy = GenerationType.IDENTITY)).

Is there anyone out the who could in a definite way clear up my mind in all these little details mentioned above?

Also, is it doable to use Eclipse's auto-generated hashCode() and equals() for the ID field for every entity? Will it be effective in every situation?

Thank you very much,

Renato

renatoaraujoc
  • 321
  • 2
  • 15
  • List .v. Set has little to do with JPA2. It is about what you need in your application. JPA2 handles both – Neil Stockton Aug 22 '14 at 16:55
  • ID is not a business key in most of cases, but rather a synthetic key. So the answer to your last questions is no as doing it this way is simply wrong. Neil has already answered about List v Set. – Vlad Aug 22 '14 at 20:25

2 Answers2

4
List versus Set

Duplicates allowed Lists allow duplicates and Sets do not allow duplicates. For some this will be the main reason for them choosing List or Set.

Multiple Bag's Exception - Multiple Eager fetching in same query One notable difference in the handling of Hibernate is that you can't fetch two different lists in a single query. It will throw an exception "cannot fetch multiple bags". But with sets, no such issues.

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
3

A list, if there is no index column specified, will just be handled as a bag by Hibernate (no specific ordering).

@OneToMany
@OrderBy("lastname ASC")
public List<Rating> ratings;

One notable difference in the handling of Hibernate is that you can't fetch two different lists in a single query. For example, if you have a Person entity having a list of contacts and a list of addresses, you won't be able to use a single query to load persons with all their contacts and all their addresses. The solution in this case is to make two queries (which avoids the cartesian product), or to use a Set instead of a List for at least one of the collections.

It's often hard to use Sets with Hibernate when you have to define equals and hashCode on the entities and don't have an immutable functional key in the entity.

furthermore i suggest you this link.

Xstian
  • 8,184
  • 10
  • 42
  • 72
  • 1
    The link you provided gave excelent information regarding the Set vs List dilemma. But could you give me some insight about mysql primary key generation and the correct implementation for hashCode and equals? Also, I'm accepting your answer as the correct one. Thanks – renatoaraujoc Aug 22 '14 at 20:19
  • @Renatinn It was a pleasure to help you :) .. in any case I suggest you another [link](http://stackoverflow.com/questions/5031614/the-jpa-hashcode-equals-dilemma) that explains the correct implementation of hashCode and equals :) – Xstian Aug 22 '14 at 23:27
  • 1
    This answer is just a copy [from another question](http://stackoverflow.com/a/6563037/3049015)? – Dominic Apr 03 '17 at 12:09
  • just a copy from https://stackoverflow.com/questions/6562673/onetomany-list-vs-set-difference. devoted – Samy Omar Jul 20 '20 at 22:35