Skip to content
Related Articles
Open in App
Not now

Related Articles

Hibernate – Difference Between List and Bag Mapping

Improve Article
Save Article
  • Last Updated : 10 Jun, 2022
Improve Article
Save Article

Hibernate supports both List and Bag Mapping and Set Mapping too. Hence there will be a tradeoff, regarding which is the best data type to use. The choice will be purely based on requirements but still, let us see the differences between them. Whenever there is a one-to-many relationship or many-to-many relationship etc., necessity, we need to have either List or Bag Mapping.

List Mapping

Let us see the configuration files while using List Mapping

XML




<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping SYSTEM
   
<hibernate-mapping>
   
 <class name="<Sample POJO class>" table="<Sample table>">
  <id name="id" type="int" column="id">
    <generator class="native"></generator>
  </id>
    
   <!-- Other properties -->
  <list name="<some collection of fields like certifications, subjects etc., >" cascade="all">
      
     <key column="id"/>
          
     <!-- This is much required as this indicates as a
           pointer for insertion/deletion/updation-->
     <list-index column="idx"/>  
      
     <one-to-many class="<sample POJO class>"/>
      
  </list>
 </class>
   
</hibernate-mapping>


Bag Mapping

Let us see the configuration files while Bag Mapping.

XML




<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC  
          "-//Hibernate/Hibernate Mapping DTD 5.3//EN"  
    
<hibernate-mapping>  
    
 <class name="<Sample POJO class>" table="<Sample table>">  
   <id name="id">  
     <generator class="increment"></generator>  
   </id>  
     
   <!--  Other properties -->
   <!--  This is almost like a list but here we are using
          bag instead of a list. Main advantage is it does 
         not have a index element -->
   <bag name="<Some collections>" table="<Sample table>">  
     <key column="id"></key>  
     <element column="<a column>" type="string"></element>  
   </bag>  
              
 </class>  
              
</hibernate-mapping>


In production deployment requirements or for the live product, from the performance point of view, we need to go for Bag mapping as the best choice. As List mapping requires ordering, database operations slow down the queries and it will take a lot of time to display the data. On the other hand, Bag mapping is the better choice and whenever there is a need for ordering, we may need to use the JPQL query with an ‘order by’ clause. Let us see the differences with Set as well with respect to database operations and hibernate

 

Order

Duplicate

Set

It  is unordered Will not allow duplicates

List

It is ordered Will allow duplicates

Bag

It is unordered Will allow duplicates

Difference Table

List Mapping

Bag Mapping

java.util package contains List. java.util package does not contain Bag.
In Hibernate, by using java.util.List we can map List. In Hibernate, by using java.util.List we can map Bag.
List is ordered and may have duplicates. Bag is unordered and allows duplicates.
This approach slows down the database operations as retrieving the associations in an ordered manner will affect the performance. This is an efficient approach as retrieving the associations like (one to many or many to more) in an unordered manner provides better performance.
By default, we get ordered results but performance-wise slow. In order to achieve order,  we may need to use JPQL Query with an order by but only when needed we can do it, and hence it is an additional one.
In older Hibernate versions (Prior to 5.0.8), while using java.util.List and merge parent entity, Hibernate generated 2 insert statements for each new child entity. It does not require an index element and is almost similar to List only. While comparing with List, Bag is preferred. 
For many-to-many associations, handling List mapping is a poor practice. Set Mapping is the better choice. Almost Bag behaves the same way as Set and depends upon the requirement, it can be decided either to go for Set or Bag
A sequence element is needed. No need for a sequence element.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!