Mapping List in Collection Mapping (using xml file)

If our industrious class has List object, we can outline List effectively either by <list> component of class in mapping document or by comment.

Here, we are utilizing the situation of Forum where one inquiry has various answers.

Mapping List in hibernate

Let's see how we can implement the list in the mapping file:

  1. <class name="com.javacodegeeks.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>  

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 5.3//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">  
  5.   
  6. <hibernate-mapping>  
  7.  <class name="com.javacodegeeks.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 5.3//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.   
  8.     <session-factory>  
  9.         <property name="hbm2ddl.auto">update</property>  
  10.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
  11.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
  12.         <property name="connection.username">system</property>  
  13.         <property name="connection.password">jtp</property>  
  14.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
  15.     <mapping resource="question.hbm.xml"/>  
  16.     </session-factory>  
  17.   
  18. </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.javacodegeeks;    
  2.     
  3. import java.util.ArrayList;    
  4.     
  5. import org.hibernate.*;  
  6. import org.hibernate.boot.Metadata;  
  7. import org.hibernate.boot.MetadataSources;  
  8. import org.hibernate.boot.registry.StandardServiceRegistry;  
  9. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
  10.       
  11. public class StoreData {    
  12.  public static void main(String[] args) {    
  13.       
  14.      StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
  15.         Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();  
  16.           
  17.         SessionFactory factory=meta.getSessionFactoryBuilder().build();  
  18.         Session session=factory.openSession();  
  19.        
  20.     Transaction t=session.beginTransaction();    
  21.         
  22.     ArrayList<String> list1=new ArrayList<String>();    
  23.     list1.add("Java is a programming language");    
  24.     list1.add("Java is a platform");    
  25.         
  26.     ArrayList<String> list2=new ArrayList<String>();    
  27.     list2.add("Servlet is an Interface");    
  28.     list2.add("Servlet is an API");    
  29.         
  30.     Question question1=new Question();    
  31.     question1.setQname("What is Java?");    
  32.     question1.setAnswers(list1);    
  33.         
  34.     Question question2=new Question();    
  35.     question2.setQname("What is Servlet?");    
  36.     question2.setAnswers(list2);    
  37.         
  38.     session.persist(question1);    
  39.     session.persist(question2);    
  40.         
  41.     t.commit();    
  42.     session.close();    
  43.     System.out.println("success");    
  44.  }    
  45. }    
    package com.javacodegeeks; import java.util.ArrayList; import org.hibernate.*; import org.hibernate.boot.Metadata; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; public class StoreData { public static void main(String[] args) { StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build(); Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build(); SessionFactory factory=meta.getSessionFactoryBuilder().build(); Session session=factory.openSession(); Transaction t=session.beginTransaction(); ArrayList<String> list1=new ArrayList<String>(); list1.add("Java is a programming language"); list1.add("Java is a platform"); ArrayList<String> list2=new ArrayList<String>(); list2.add("Servlet is an Interface"); list2.add("Servlet is an API"); Question question1=new Question(); question1.setQname("What is Java?"); question1.setAnswers(list1); Question question2=new Question(); question2.setQname("What is Servlet?"); question2.setAnswers(list2); session.persist(question1); session.persist(question2); t.commit(); session.close(); System.out.println("success"); } }





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