Hibernate – Collection Mapping

Collection elements are much needed to have one-to-many, many-to-many, relationships, etc., Any one type from below can be used to declare the type of collection in the Persistent class. Persistent class from one of the following types:

  • java.util.List
  • java.util.Set
  • java.util.SortedSet
  • java.util.Map
  • java.util.SortedMap
  • java.util.Collection

Let us see where can we use List, Set, and Map by seeing the differences among them

List

Set

Map

Duplicates are allowed. Duplicates are not allowed. Duplicates are not allowed.
Insertion order is maintained. Insertion order is not maintained. Insertion order is not maintained.
Index element helps to identify an element in the list. Hence need to use this whenever the index is possible and it is mandatory. For uniquely maintaining the collection of elements, we can go for the set. As a key/value pair pattern means we can go for Map.

Mapping List

POJO class

Java

import java.util.List;

  

public class<POJO class> {

    private int id;

    private String variableString;

    

    private List<String>

        collectionItems;

                         

                         

                         

                         

                         

    private List < Another POJO class

    > collectionItems1

  

    

}

XML

XML

<class name="<Respective POJO class>" table="<Respective table>">  

    <id name="id">  

     <generator class="increment"></generator>  

    </id>  

      

      

    <list name="<collectionItems>" table="<Respective child table>">  

    

    

    <key column="id"></key>  

    

    

    <index column="type"></index>  

    

    

    <element column="answer" type="string"></element></list>  

    

      

    <list name="<collectionItems>" >  

          <key column="id"></key>  

          <index column="type"></index>  

          <one-to-many class="<Respective child POJO class>" />  

    </list>  

</class>

In the main java file where we can do CRUD operations, we have to associate a List as follows

Main java file

Java

ArrayList<String> list1 = new ArrayList<String>();

list1.add("example1");

list1.add("example2");

Not only List, but we can also use Bag, Set, and Map in the collections.

Mapping Bag

This is almost similar to List but the index element is not needed

Example of POJO class

Java

import java.util.List;

  

public class<POJO class> {

    private int id;

    private String variableString;

    

    private List<String>

        collectionItems;

                         

                         

                         

                         

                         

    private List < Another POJO class

    > collectionItems1

  

    

}

In Hibernate Mapping, it differs from the List

XML

<bag name="<collectionname>" table="<Respective POJO class>">  

     <key column="<foreignKeyId>"></key>  

     

     <element column="<collectionColumn>" type="string"></element>  

</bag>

The Main Java file is similar to the one that is used for the list.

Major difference between List and Bag: In List, the index element is mandatory, and order is maintained. In Bag, there is no index element and hence no order.

Mapping Set

XML

XML

<class name="<POJO>" table="<Respective table>">  

       ...        

          <set name="<collection Items>" table="<child table>">  

          <key column="id"></key>  

          <element column="<collection column>" type="string"></element>  

          </set>  

       ...  

</class>

POJO class

Java

import java.util.Set;

  

public class<POJO> {

    private int id;

    

    

    private Set<String> collectionItems;

  

    

}

Main java file

Java

HashSet<String> set1 = new HashSet<String>();

set1.add("example1");

set1.add("example2");

Mapping Map

XML

XML

<class ....>

  ....

  <map name="<collectionitems>" table="<table>" cascade="all">  

  <key column="id"></key>  

  

  <index column="<columnname>" type="string"></index>  

  <element column="<columnname>" type="string"></element>  

  </map>  

  ...

</class>  

POJO class

Java

import java.util.Map;

  

public class<POJO> {

    private int id;

    

    

    private Map<String, String> collectionItems;

Main java file

Java

HashMap<String, String> map1

    = new HashMap<String, String>();

map1.put("key1", "value1");

map1.put("key2", "value2");

SortedSet and SortedMap by the name itself we can come to know that if a set is sorted(ascending order by default)/a map is sorted(ascending order by default)

Mapping SortedSet

POJO class

Java

private SortedSet collectionItems;

XML

XML

      

<set name = "<collectionItems>" cascade="all" sort="<POJO class>">

         <key column = "id"/>

         <one-to-many class="<child POJO class>"/>

      </set>

Main java file

Java

TreeSet set2 = new TreeSet();

set2.add(new<Child POJO class>(<value>));

Similarly, SortedMap is similar to the map and by default, it follows natural sorting order or we can customize that

Mapping SortedMap

POJO class

Java

private SortedMap collectionItems;

XML

XML

<map name="collectionItems" cascade="all"  sort="natural" >

       <key column="id"/>

       <index column="indexColumn"  type="string"/>

       <one-to-many class="<child POJO class>"/>

</map>

Main Java file

Java

TreeMap map = new TreeMap();

map.put("key1", value1);

map.put("key2", value2);

map.put("key3", value3);

Conclusion

By using different patterns, according to the requirements, we can use collection mapping in hibernate.

  • For ordering, use List
  • For uniqueness, use Set
  • For key-value maintenance use Map
  • Using SortedMap/SortedSet is not an ideal solution as it will degrade performance. Instead, we can go for JPQL and add an Order By clause.