What is the difference between using a Set or a Collection for @OneToMany or @ManyToMany properties on my hibernate entity objects?
Does Hibernate map things differently depending on which one you choose?
What is the difference between using a Set or a Collection for @OneToMany or @ManyToMany properties on my hibernate entity objects?
Does Hibernate map things differently depending on which one you choose?
In the context of hibernate, following is the scenario under which you would use Set instead of Collection: -
"From Order as orders left fetch join orders.orderLineItems as orderLineItems ORDER BY orders.id DESC"
It returns duplicates so use the hash set to remove them.
Query query = session.getNamedQuery("OrdersAndLoadLineItems");
Set set = new LinkedHashSet();
set.addAll(query.list());
return set;
This is taken from Hibernate FAQ link is http://www.hibernate.org/117.241.html
If you look at the API Set extends Collection. According, to the description the Set does not allow null values.
A Collection is something more general than a Set. A Set is a more specific subinterface of a Collection. See here.