Hibernate Many to Many Example using XML

We can map many to many connection either utilizing rundown, set, pack, map, and so on. Here, we are going to utilize list for many-to-many mapping. In such case, three tables will be created.

Example of Many to Many Mapping

In this precedent, we will create a many to many connection among inquiries and answers utilizing list.

1) Create the Persistent class

There are two relentless classes Question.java and Answer.java. Question class contains Answer class reference and bad habit versa.

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. public int getId() {  
  11.     return id;  
  12. }  
  13. public void setId(int id) {  
  14.     this.id = id;  
  15. }  
  16. public String getQname() {  
  17.     return qname;  
  18. }  
  19. public void setQname(String qname) {  
  20.     this.qname = qname;  
  21. }  
  22. public List<Answer> getAnswers() {  
  23.     return answers;  
  24. }  
  25. public void setAnswers(List<Answer> answers) {  
  26.     this.answers = answers;  
  27. }      
  28. }  

Answer.java

  1. package com.javacodegeeks;  
  2.   
  3. import java.util.*;  
  4.   
  5. public class Answer {    
  6. private int id;    
  7. private String answername;    
  8. private String postedBy;    
  9. private List<Question> questions;  
  10. public int getId() {  
  11.     return id;  
  12. }  
  13. public void setId(int id) {  
  14.     this.id = id;  
  15. }  
  16. public String getAnswername() {  
  17.     return answername;  
  18. }  
  19. public void setAnswername(String answername) {  
  20.     this.answername = answername;  
  21. }  
  22. public String getPostedBy() {  
  23.     return postedBy;  
  24. }  
  25. public void setPostedBy(String postedBy) {  
  26.     this.postedBy = postedBy;  
  27. }  
  28. public List<Question> getQuestions() {  
  29.     return questions;  
  30. }  
  31. public void setQuestions(List<Question> questions) {  
  32.     this.questions = questions;  
  33. }    
  34. }  

2) Create the Mapping file for the persistent class

Here, we have created the question.hbm.xml and answer.hbm.xml file for defining the list.

question.hbm.xml

  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.   
  8.   <class name="com.javacodegeeks.Question" table="ques1911">  
  9.         <id name="id" type="int">  
  10.             <column name="q_id" />  
  11.             <generator class="increment" />  
  12.         </id>  
  13.         <property name="qname" />  
  14.   
  15.         <list name="answers" table="ques_ans1911" fetch="select" cascade="all">  
  16.             <key column="q_id" />  
  17.                <index column="type"></index>   
  18.             <many-to-many class="com.javacodegeeks.Answer" column="ans_id" />  
  19.         </list>  
  20.     </class>   
  21. </hibernate-mapping>         

answer.hbm.xml

  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.Answer" table="ans1911">  
  8.         <id name="id" type="int">  
  9.             <column name="ans_id" />  
  10.             <generator class="increment" />  
  11.         </id>  
  12.         <property name="answername"  />  
  13.         <property name="postedBy" />  
  14.     </class>  
  15. </hibernate-mapping>  

3) Create the configuration file

This file contains information about the database and mapping file.

hibernate.cfg.xml

  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">create</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.     <mapping resource="answer.hbm.xml"/>  
  17.     </session-factory>    
  18.     
  19. </hibernate-configuration>    

4) Create the class to store the data

StoreData.java

  1. package com.javacodegeeks;    
  2. import java.util.ArrayList;    
  3. import org.hibernate.*;  
  4. import org.hibernate.boot.Metadata;  
  5. import org.hibernate.boot.MetadataSources;  
  6. import org.hibernate.boot.registry.StandardServiceRegistry;  
  7. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
  8.      
  9. public class StoreData {    
  10. public static void main(String[] args) {    
  11.      
  12.     StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();    
  13.     Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();    
  14.     SessionFactory factory=meta.getSessionFactoryBuilder().build();    
  15.     Session session=factory.openSession();    
  16.     Transaction t=session.beginTransaction();      
  17.           
  18.     Answer ans1=new Answer();  
  19.     ans1.setAnswername("Java is a programming language");  
  20.     ans1.setPostedBy("Ravi Malik");  
  21.       
  22.     Answer ans2=new Answer();  
  23.     ans2.setAnswername("Java is a platform");  
  24.     ans2.setPostedBy("Ramon Kumar");  
  25.      
  26.     Question q1=new Question();  
  27.     q1.setQname("What is Java?");  
  28.     ArrayList<Answer> l1=new ArrayList<Answer>();  
  29.     l1.add(ans1);  
  30.     l1.add(ans2);  
  31.     q1.setAnswers(l1);  
  32.       
  33.     Answer ans3=new Answer();    
  34.     ans3.setAnswername("Servlet is an Interface");    
  35.     ans3.setPostedBy("Rajesh Kumar");    
  36.             
  37.     Answer ans4=new Answer();    
  38.     ans4.setAnswername("Servlet is an API");    
  39.     ans4.setPostedBy("Arun");    
  40.       
  41.     Question q2=new Question();  
  42.     q2.setQname("What is Servlet?");  
  43.     ArrayList<Answer> l2=new ArrayList<Answer>();  
  44.     l2.add(ans3);  
  45.     l2.add(ans4);  
  46.     q2.setAnswers(l2);  
  47.     session.persist(q1);    
  48.     session.persist(q2);    
  49.         
  50.     t.commit();    
  51.     session.close();    
  52.     System.out.println("success");        
  53. }    
  54. }     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(); Answer ans1=new Answer(); ans1.setAnswername("Java is a programming language"); ans1.setPostedBy("Anglina"); Answer ans2=new Answer(); ans2.setAnswername("Java is a platform"); ans2.setPostedBy("Ramon Kumar"); Question q1=new Question(); q1.setQname("What is Java?"); ArrayList<Answer> l1=new ArrayList<Answer>(); l1.add(ans1); l1.add(ans2); q1.setAnswers(l1); Answer ans3=new Answer(); ans3.setAnswername("Servlet is an Interface"); ans3.setPostedBy("Rajesh Kumar"); Answer ans4=new Answer(); ans4.setAnswername("Servlet is an API"); ans4.setPostedBy("Arun"); Question q2=new Question(); q2.setQname("What is Servlet?"); ArrayList<Answer> l2=new ArrayList<Answer>(); l2.add(ans3); l2.add(ans4); q2.setAnswers(l2); session.persist(q1); session.persist(q2); 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