Hibernate Tutorial: One To Many Mapping in Hibernate by List Example

If the persistent class has list object that contains the element reference, we have to utilize one-to-numerous relationship to outline list component.

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

Full example of One to Many mapping in Hibernate by List

In this example, we are going to see full example of mapping list that contains entity reference.

1) Create the Persistent class

This persistent class defines properties of the class including List.

Question.java

  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<Answer> answers;  
  9.   
  10. //getters and setters  
  11.   
  12. }  

Answer.java

  1. package com.javacodegeeks;  
  2.   
  3. public class Answer {  
  4. private int id;  
  5. private String answername;  
  6. private String postedBy;  
  7. //getters and setters  
  8.   
  9. }  
  10. }  

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="q501">  
  8.           <id name="id">  
  9.           <generator class="increment"></generator>  
  10.           </id>  
  11.           <property name="qname"></property>  
  12.             
  13.           <list name="answers" cascade="all">  
  14.           <key column="qid"></key>  
  15.           <index column="type"></index>  
  16.           <one-to-many class="com.javacodegeeks.Answer"/>  
  17.           </list>  
  18.             
  19.           </class>  
  20.             
  21.           <class name="com.javacodegeeks.Answer" table="ans501">  
  22.           <id name="id">  
  23.           <generator class="increment"></generator>  
  24.           </id>  
  25.           <property name="answername"></property>  
  26.           <property name="postedBy"></property>  
  27.           </class>  
  28.             
  29.           </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. <!-- 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">jtp</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.javacodegeeks;    
  2.   
  3. import java.util.ArrayList;    
  4. import org.hibernate.Session;  
  5. import org.hibernate.SessionFactory;  
  6. import org.hibernate.Transaction;  
  7. import org.hibernate.boot.Metadata;  
  8. import org.hibernate.boot.MetadataSources;  
  9. import org.hibernate.boot.registry.StandardServiceRegistry;  
  10. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
  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.     Answer ans1=new Answer();    
  23.     ans1.setAnswername("Java is a programming language");    
  24.     ans1.setPostedBy("Ravi Malik");    
  25.         
  26.     Answer ans2=new Answer();    
  27.     ans2.setAnswername("Java is a platform");    
  28.     ans2.setPostedBy("Sudhir Kumar");    
  29.         
  30.     Answer ans3=new Answer();    
  31.     ans3.setAnswername("Servlet is an Interface");    
  32.     ans3.setPostedBy("Jai Kumar");    
  33.         
  34.     Answer ans4=new Answer();    
  35.     ans4.setAnswername("Servlet is an API");    
  36.     ans4.setPostedBy("Arun");    
  37.         
  38.     ArrayList<Answer> list1=new ArrayList<Answer>();    
  39.     list1.add(ans1);    
  40.     list1.add(ans2);    
  41.         
  42.     ArrayList<Answer> list2=new ArrayList<Answer>();    
  43.     list2.add(ans3);    
  44.     list2.add(ans4);    
  45.         
  46.     Question question1=new Question();    
  47.     question1.setQname("What is Java?");    
  48.     question1.setAnswers(list1);    
  49.         
  50.     Question question2=new Question();    
  51.     question2.setQname("What is Servlet?");    
  52.     question2.setAnswers(list2);    
  53.         
  54.     session.persist(question1);    
  55.     session.persist(question2);    
  56.         
  57.     t.commit();    
  58.     session.close();    
  59.     System.out.println("success");    
  60. }    
  61. }    






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