Hibernate Tutorial
Core Hibernate
Hibernate Mapping
Hibernate Annotations
Hibernate with Spring Framework
Hibernate with Database
Hibernate Log4j
Inheritance Mapping
In Hibernate, both the list
and bag
are collection mappings used to map Java collection types to database tables. However, they have some notable differences, and understanding these can be crucial for making the right mapping decisions.
List: Represents an ordered collection where the order is maintained using an index. A Java List
is mapped using <list>
in Hibernate mapping files.
Bag: Represents an unordered collection where duplicates are allowed. A Java List
can also be mapped as a bag using <bag>
in Hibernate mapping files.
List: Order is important. Hibernate maintains the order of elements using an additional index column. This index column keeps track of the position of each element.
Bag: Order is not guaranteed. Hibernate doesn't maintain the order of elements, and no index column is used.
List: Since Hibernate maintains the order using an index column, updating an item in the list might result in updating index values for other elements as well. This can lead to performance issues especially if there are a lot of updates or the list is large.
Bag: Since order isn't maintained, updates might be more efficient compared to lists.
list
and bag
allow duplicates. However, the main difference is in the ordering.List: Use this when the order of elements is important and you need to maintain the sequence of elements.
Bag: Use this when the order of elements is not crucial, and you are more concerned about performance.
Here's a basic idea of how each is mapped:
List Mapping:
<list name="items" table="ITEM_TABLE" cascade="all"> <key column="parent_id"/> <list-index column="index_column"/> <one-to-many class="Item"/> </list>
Bag Mapping:
<bag name="items" table="ITEM_TABLE" cascade="all"> <key column="parent_id"/> <one-to-many class="Item"/> </bag>
In summary, whether to use a list
or a bag
primarily depends on the requirements of your application. If order is crucial, use a list. If not, a bag might provide better performance, especially with frequent updates.
Hibernate list vs bag mapping example:
// List mapping example @Entity public class ParentEntity { @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) @OrderColumn(name = "list_order") private List<ChildEntity> children = new ArrayList<>(); } // Bag mapping example @Entity public class ParentEntity { @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) private List<ChildEntity> children = new ArrayList<>(); }
Hibernate indexed list mapping:
@Entity public class ParentEntity { @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) @OrderColumn(name = "list_index") private List<ChildEntity> children = new ArrayList<>(); }
Hibernate bag mapping cascade options:
CascadeType.ALL
, CascadeType.PERSIST
, CascadeType.MERGE
, etc.// Example with CascadeType.ALL @Entity public class ParentEntity { @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) private List<ChildEntity> children = new ArrayList<>(); }