Collection Mapping in Hibernate

We can map collection elements of Persistent class in Hibernate. You need to declare the type of collection in 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
  • or write the implementation of org.hibernate.usertype.UserCollectionType
The persistent class should be defined like this for collection element.
  1. package com.javatpoint;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class Question {  
  6. private int id;  
  7. private String qname;  
  8. private List<String> answers;//List can be of any type  
  9.   
  10. //getters and setters  
  11.   
  12. }  

Mapping collection in mapping file

There are many subelements of <class> elements to map the collection. They are <list>, <bag>, <set> and <map>. Let's see how we implement the list for the above class:
  1. <class name="com.javatpoint.Question" table="q100">  
  2.           <id name="id">  
  3.           <generator class="increment"></generator>  
  4.           </id>  
  5.           <property name="qname"></property>  
  6.             
  7.           <list name="answers" table="ans100">  
  8.           <key column="qid"></key>  
  9.           <index column="type"></index>  
  10.           <element column="answer" type="string"></element>  
  11.           </list>  
  12.             
  13.           </class>  
There are three subelements used in the list:
  • <key> element is used to define the foreign key in this table based on the Question class identifier.
  • <index> element is used to identify the type. List and Map are indexed collection.
  • <element> is used to define the element of the collection.
This is the mapping of collection if collection stores string objects. But if collection stores entity reference (another class objects), we need to define <one-to-many> or <many-to-many> element. Now the Persistent class will look like:
  1. package com.javatpoint;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class Question {  
  6. private int id;  
  7. private String qname;  
  8. private List<Answer> answers;//Here, List stores the objects of Answer class  
  9.   
  10. //getters and setters  
  11.   
  12. }  
  13. </tetarea></div>  
  14.   
  15. <div class="codeblock"><textarea name="code" class="java" >  
  16.   
  17. package com.javatpoint;  
  18.   
  19. import java.util.List;  
  20.   
  21. public class Answer {  
  22. private int id;  
  23. private String answer;  
  24. private String posterName;  
  25.   
  26. //getters and setters  
  27.   
  28. }  
  29. </pre></div>  
  30.   
  31. <p>Now the mapping file will be:</p>  
  32. <div class="codeblock"><textarea name="code" class="java" >  
  33.   
  34. <class name="com.javatpoint.Question" table="q100">  
  35.           <id name="id">  
  36.           <generator class="increment"></generator>  
  37.           </id>  
  38.           <property name="qname"></property>  
  39.             
  40.           <list name="answers" >  
  41.           <key column="qid"></key>  
  42.           <index column="type"></index>  
  43.           <one-to-many class="com.javatpoint.Answer" />  
  44.           </list>  
  45.             
  46.           </class>  
Here, List is mapped by one-to-many relation. In this scenario, there can be many answers for one question.

Understanding key element

The key element is used to define the foreign key in the joined table based on the original identity. The foreign key element is nullable by default. So for non-nullable foreign key, we need to specify not-null attribute such as:
  1. <key column="qid" not-null="true" ></key>  
The attributes of the key element are column, on-delete, property-ref, not-null, update and unique.
  1. <key  
  2. column="columnname"  
  3. on-delete="noaction|cascade"  
  4. not-null="true|false"  
  5. property-ref="propertyName"  
  6. update="true|false"  
  7. unique="true|false"  
  8. />  

Indexed collections

The collection elements can be categorized in two forms:
  • indexed ,and
  • non-indexed
The List and Map collection are indexed whereas set and bag collections are non-indexed. Here, indexed collection means List and Map requires an additional element <index>.

Collection Elements

The collection elements can have value or entity reference (another class object). We can use one of the 4 elements
  • element
  • component-element
  • one-to-many, or
  • many-to-many
The element and component-element are used for normal value such as string, int etc. whereas one-to-many and many-to-many are used to map entity reference.

Upcoming topics in Collection Mapping

Mapping List In this example, we are going to map the List element.
Mapping Bag In this example, we are going to use the bag collection of Hibernate framework.
Mapping Set Here, we will map the set element of collection.