Hibernate Collection Mapping Tutorial

We can outline components of Persistent class in Hibernate. You have to announce the kind of accumulation in Persistent class from one of the accompanying types:

  • java.util.List
  • java.util.Set
  • java.util.SortedSet
  • java.util.Map
  • java.util.SortedMap
  • java.util.Collection
  • or compose the usage of org.hibernate.usertype.UserCollectionType

The steady class ought to be characterized like this for gathering element.

  1. package com.javacodegeeks;  
  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.javacodegeeks.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> component is utilized to characterize the remote key in this table dependent on the Question class identifier.
  • <index> component is utilized to recognize the sort. Rundown and Map are ordered collection.
  • <element> is utilized to characterize the component of the collection.
  • This is the mapping of gathering if accumulation stores string objects. However, in the event that gathering stores element reference (another class objects), we have to characterize <one-to-many> or <many-to-many> component. Presently the Persistent class will resemble:

    package com.javacodegeeks;  
  •   
  • import java.util.List;  
  •   
  • public class Question {  
  • private int id;  
  • private String qname;  
  • private List<Answer> answers;//Here, List stores the objects of Answer class  
  •   
  • //getters and setters  
  •   
  • }  
    1. package com.javacodegeeks;  
    2. import java.util.List;  
    3. public class Answer {  
    4. private int id;  
    5. private String answer;  
    6. private String posterName;  
    7. //getters and setters  
    8. }  

    Now the mapping file will be:

    1. <class name="com.javacodegeeks.Question" table="q100">  
    2.           <id name="id">  
    3.           <generator class="increment"></generator>  
    4.           </id>  
    5.           <property name="qname"></property>  
    6.             
    7.           <list name="answers" >  
    8.           <key column="qid"></key>  
    9.           <index column="type"></index>  
    10.           <one-to-many class="com.javacodegeeks.Answer" />  
    11.           </list>  
    12.             
    13.           </class>   <class name="com.javacodegeeks.Question" table="q100"> <id name="id"> <generator class="increment"></generator> </id> <property name="qname"></property> <list name="answers" > <key column="qid"></key> <index column="type"></index> <one-to-many class="com.javacodegeeks.Answer" /> </list> </class>





    javacodegeeks is optimized for learning.© javacodegeeks .
    All Right Reserved and you agree to have read and accepted our term and condition