Mapping List in Collection Mapping (using xml file)

If our persistent class has List object, we can map the List easily either by <list> element of class in mapping file or by annotation.
Here, we are using the scenario of Forum where one question has multiple answers.
Mapping List in hibernate Let's see how we can implement the list in the mapping file:
  1. <class name="com.javatpoint.Question" table="q100">  
  2.        ...        
  3.           <list name="answers" table="ans100">  
  4.           <key column="qid"></key>  
  5.           <index column="type"></index>  
  6.           <element column="answer" type="string"></element>  
  7.           </list>  
  8.             
  9.        ...  
  10. </class>  

List and Map are index based collection, so an extra column will be created in the table for indexing.


Example of mapping list in collection mapping

In this example, we are going to see full example of collection mapping by list. This is the example of List that stores string value not entity reference that is why are going to use element instead of one-to-many within the list element.

1) create the Persistent class

This persistent class defines properties of the class including List.
  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;  
  9.   
  10. //getters and setters  
  11.   
  12. }  

2) create the Mapping file for the persistent class

Here, we have created the question.hbm.xml file for defining the list.
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.           "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping>  
  7.  <class name="com.javatpoint.Question" table="q100">  
  8.    <id name="id">  
  9.      <generator class="increment"></generator>  
  10.    </id>  
  11.    <property name="qname"></property>  
  12.             
  13.    <list name="answers" table="ans100">  
  14.      <key column="qid"></key>  
  15.      <index column="type"></index>  
  16.      <element column="answer" type="string"></element>  
  17.    </list>  
  18.             
  19.  </class>  
  20.             
  21. </hibernate-mapping>  

3) create the configuration file

This file contains information about the database and mapping file.
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.   
  6. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
  7. <hibernate-configuration>  
  8.   
  9.     <session-factory>  
  10.         <property name="hbm2ddl.auto">update</property>  
  11.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
  12.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
  13.         <property name="connection.username">system</property>  
  14.         <property name="connection.password">oracle</property>  
  15.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
  16.     <mapping resource="question.hbm.xml"/>  
  17.     </session-factory>  
  18.   
  19. </hibernate-configuration>  

4) Create the class to store the data

In this class we are storing the data of the question class.
  1. package com.javatpoint;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import org.hibernate.*;  
  6. import org.hibernate.cfg.*;  
  7.   
  8. public class StoreData {  
  9.  public static void main(String[] args) {  
  10.     Session session=new Configuration().configure("hibernate.cfg.xml")  
  11.                         .buildSessionFactory().openSession();  
  12.     Transaction t=session.beginTransaction();  
  13.       
  14.     ArrayList<String> list1=new ArrayList<String>();  
  15.     list1.add("java is a programming language");  
  16.     list1.add("java is a platform");  
  17.       
  18.     ArrayList<String> list2=new ArrayList<String>();  
  19.     list2.add("Servlet is an Interface");  
  20.     list2.add("Servlet is an API");  
  21.       
  22.     Question question1=new Question();  
  23.     question1.setQname("What is Java?");  
  24.     question1.setAnswers(list1);  
  25.       
  26.     Question question2=new Question();  
  27.     question2.setQname("What is Servlet?");  
  28.     question2.setAnswers(list2);  
  29.       
  30.     session.persist(question1);  
  31.     session.persist(question2);  
  32.       
  33.     t.commit();  
  34.     session.close();  
  35.     System.out.println("success");  
  36.  }  
  37. }  

How to fetch the data of List

Here, we have used HQL to fetch all the records of Question class including answers. In such case, it fetches the data from two tables that are functional dependent.
  1. package com.javatpoint;  
  2.   
  3. import java.util.*;  
  4.   
  5. import org.hibernate.*;  
  6. import org.hibernate.cfg.*;  
  7.   
  8. public class FetchData {  
  9. public static void main(String[] args) {  
  10.       
  11.     Session session=new Configuration().configure("hibernate.cfg.xml")  
  12.                         .buildSessionFactory().openSession();  
  13.       
  14.     Query query=session.createQuery("from Question");  
  15.     List<Question> list=query.list();  
  16.       
  17.     Iterator<Question> itr=list.iterator();  
  18.     while(itr.hasNext()){  
  19.         Question q=itr.next();  
  20.         System.out.println("Question Name: "+q.getQname());  
  21.           
  22.         //printing answers  
  23.         List<String> list2=q.getAnswers();  
  24.         Iterator<String> itr2=list2.iterator();  
  25.         while(itr2.hasNext()){  
  26.             System.out.println(itr2.next());  
  27.         }  
  28.           
  29.     }  
  30.     session.close();  
  31.     System.out.println("success");  
  32.       
  33. }  
  34. }