1
1
|
What is the main difference between the
Set and Bag collections in Hibernate? In what scenarios should we use Set and Bag ? | |||
add comment |
2
|
From the Hibernate reference:
And also:
| ||
add comment |
1
|
A
is an unordered collection, which can contain duplicated elements. That means if you persist a bag with some order of elements, you cannot expect the same order retains when the collection is retrieved. There is not a “bag” concept in Java collections framework, so we just use ajava.util.List corresponds to a .
A
is similar to except that it can only store unique objects. That means no duplicate elements can be contained in a set. When you add the same element to a set for second time, it will replace the old one. A set is unordered by default but we can ask it to be sorted. The corresponding type of a in Java is java.util.Set .
Examples
Mapping
|
Mapping
name="employees" table="employee"
inverse="true" lazy="true" fetch="select">
name="employee_id" not-null="true" />
Thus, both are mapped exactly same way in hbm file. But differs only in the way it handles duplicate records.
0
|
Both are unordered collections. Bags allow duplicates. Sets do not.
|
List
andSet
interfaces. – AVD Dec 11 '12 at 1:37