Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaHibernate – Collection Mapping

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;
    // other data members
    private List<String>
        collectionItems; // List can be of any type
                         // if we use another POJO class in
                         // the list to have one to many/many
                         // to many relationships We need to
                         // define Another POJO class in this
                         // case
    private List < Another POJO class
    > collectionItems1
  
    // getters and setters
}


XML

XML




<class name="<Respective POJO class>" table="<Respective table>">  
    <id name="id">  
     <generator class="increment"></generator>  
    </id>  
      
      <!--other properties -->
    <list name="<collectionItems>" table="<Respective child table>">  
    
    <!-- foreign key in child table that refers to Primary table -->
    <key column="id"></key>  
    
    <!-- For identification. List/Map are indexed collection -->
    <index column="type"></index>  
    
    <!-- collection element of type element> -->
    <element column="answer" type="string"></element></list>  
    
      <!-- If we are using one to many relationship -->
    <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




// Rest of code ....
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;
    // other data members
    private List<String>
        collectionItems; // List can be of any type
                         // if we use another POJO class in
                         // the list to have one to many/many
                         // to many relationships We need to
                         // define Another POJO class in this
                         // case
    private List < Another POJO class
    > collectionItems1
  
    // getters and setters
}


In Hibernate Mapping, it differs from the List

XML




<!-- Rest of the properties -->
<bag name="<collectionname>" table="<Respective POJO class>">  
     <key column="<foreignKeyId>"></key>  
     <!-- Here we do not have index, that is the difference with the list -->
     <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;
    // other data members
    // Collection item has to be represented via Set
    private Set<String> collectionItems;
  
    // getters and setters
}


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>  
  <!-- In List and Map, index column is required -->
  <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;
    // other data members
    // a key value is maintained
    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




<!-- .. -->
<!-- Here this POJO class should implement comparator
      or we can leave it as natural-->
<set name = "<collectionItems>" cascade="all" sort="<POJO class>">
         <key column = "id"/>
         <one-to-many class="<child POJO class>"/>
      </set>
<!-- .... -->


Main java file

Java




// we can use Treeset
//....
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.
RELATED ARTICLES

Most Popular

Recent Comments