13

I need to know how to use the bag tag and what is the purpose of it?

Tom11
  • 2,419
  • 8
  • 30
  • 56
gokul
  • 153
  • 1
  • 3
  • 11

3 Answers3

8

For collection mapping

If your table does not have an index column, and you still wish to use List as the property type, you can map the property as a Hibernate < bag>. A bag does not retain its order when it is retrieved from the database, but it can be optionally sorted or ordered.

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html

3

From the API:

A bag is an unordered, unkeyed collection that can contain the same element multiple times. A bag permits duplicates, so it has no primary key. The Java collections API, curiously, has no Bag.

McGin
  • 1,361
  • 2
  • 13
  • 31
2

A short and simple answer:

You don't need to use the <index> tag when mapping an unsorted List and you also don't need an additional index column in database.

Unlike a List a Bag does not persist the order of its elements, but you can specify an order-by parameter to retrieve its elements in a specific order.

Rafael Borja
  • 4,487
  • 7
  • 29
  • 33