27

What is the main difference between the Set and Bag collections in Hibernate? In what scenarios should we use Set and Bag?

1
  • 1
    Read the documentation of List and Set interfaces. Dec 11, 2012 at 1:37

4 Answers 4

28

A <bag> 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 a java.util.List corresponds to a <bag>.

A <set> is similar to <bag> 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 <set>

 <set name="employees" table="employee"
            inverse="true" lazy="true" fetch="select">
        <key>
            <column name="department_id" not-null="true" />
        </key>
        <one-to-many class="net.viralpatel.hibernate.Employee" />
    </set>

Mapping <bag>

  <bag name="employees" table="employee"
                inverse="true" lazy="true" fetch="select">
            <key>
                <column name="employee_id" not-null="true" />
            </key>
            <one-to-many class="net.viralpatel.hibernate.Employee" />
        </bag>

Thus, both are mapped exactly same way in hbm file. But differs only in the way it handles duplicate records.

Source: Hibernate One to Many XML Tutorial

3
  • 1
    In case of annotations, where and what should be the mapping attribute to mark the collection of bag type??
    – Arun Kumar
    Jun 11, 2014 at 12:07
  • 1
    For e.g if i have @OneToMany(mappedBy="Dept") private List<Employee> employees; in Department entity. how can i mark the collection type as bag with annotations??
    – Arun Kumar
    Jun 11, 2014 at 12:13
  • It's not true in Java that an element will be replaced in a set if you try to add a duplicate as mentioned above: "add the same element to a set for second time, it will replace the old one". It's not clear to me If the author is writing about a Hibernate specific behavior that I'm not aware of however. Mar 13, 2016 at 3:36
8

From the Hibernate reference:

Bags are the worst case since they permit duplicate element values and, as they have no index column, no primary key can be defined. Hibernate has no way of distinguishing between duplicate rows.

And also:

There is a particular case, however, in which bags, and also lists, are much more performant than sets. For a collection with inverse="true", the standard bidirectional one-to-many relationship idiom, for example, we can add elements to a bag or list without needing to initialize (fetch) the bag elements.

7

Quick Summary difference between the various collections is as below

  • Set - No duplicates and No order
  • Bag - Can contain Duplicates and No Order (Also called, Unordered List or Set with duplicates)

  • List - Can contain Duplicates but with Order preserved

    • Can be created using @OrderBy to preserve order
2

Both are unordered collections. Bags allow duplicates. Sets do not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.