Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

One to Many by Bag


If the persistent class has list object that contains the entity reference, we need to use one-to-many association to map the list element. We can map this list object by either list or bag.

Notice that bag is not index-based whereas list is index-based.

Here, we are using the scenario of Forum where one question has multiple answers.

onetomanyrelation1

Let’s see the persistent class that has list objects. In this case, there can be many answers for a question and each answer may have its own informations that is why we have used list element (containing Answer objects) to represent a collection of answers.

  1. package com.Kreationnext;
  2. import java.util.List;
  3. public class Question {
  4. private int id;
  5. private String qname;
  6. private List<Answer> answers;
  7. //getters and setters
  8. }

The Answer class has its own informations such as id, answername, postedBy etc.

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

The Question class has list object that have entity reference (i.e. Answer class object). In such case, we need to use one-to-many of bag to map this object. Let’s see how we can map it.

  1. <bag name=“answers” cascade=“all”>
  2.           <key column=“qid”></key>
  3.           <one-to-many class=“com.Kreationnext.Answer”/>
  4. </list>

Example of mapping bag in collection mapping by one to many association

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.Kreationnext;
  2. import java.util.List;
  3. public class Question {
  4. private int id;
  5. private String qname;
  6. private List<Answer> answers;
  7. //getters and setters
  8. }

Answer.java

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

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.           <hibernate-mapping>
  6.           <class name=“com.Kreationnext.Question” table=“q501”>
  7.           <id name=“id”>
  8.           <generator class=“increment”></generator>
  9.           </id>
  10.           <property name=“qname”></property>
  11.           <bag name=“answers” cascade=“all”>
  12.           <index column=“type”></index>
  13.           <one-to-many class=“com.Kreationnext.Answer”/>
  14.           </bag>
  15.           </class>
  16.           <class name=“com.Kreationnext.Answer” table=“ans501”>
  17.           <id name=“id”>
  18.           <generator class=“increment”></generator>
  19.           </id>
  20.           <property name=“answername”></property>
  21.           <property name=“postedBy”></property>
  22.           </class>
  23.           </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. <!– Generated by MyEclipse Hibernate Tools.                   –>
  6. <hibernate-configuration>
  7.     <session-factory>
  8.         <property name=“hbm2ddl.auto”>update</property>
  9.         <property name=“dialect”>org.hibernate.dialect.Oracle9Dialect</property>
  10.         <property name=“connection.url”>jdbc:oracle:thin:@localhost:1521:xe</property>
  11.         <property name=“connection.username”>system</property>
  12.         <property name=“connection.password”>oracle</property>
  13.         <property name=“connection.driver_class”>oracle.jdbc.driver.OracleDriver</property>
  14.     <mapping resource=“question.hbm.xml”/>
  15.     </session-factory>
  16. </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.Kreationnext;
  2. import java.util.ArrayList;
  3. import org.hibernate.*;
  4. import org.hibernate.cfg.*;
  5. public class StoreData {
  6. public static void main(String[] args) {
  7.     Session session=new Configuration().configure(“hibernate.cfg.xml”)
  8.                             .buildSessionFactory().openSession();
  9.     Transaction t=session.beginTransaction();
  10.     Answer ans1=new Answer();
  11.     ans1.setAnswername(“java is a programming language”);
  12.     ans1.setPostedBy(“Ravi Malik”);
  13.     Answer ans2=new Answer();
  14.     ans2.setAnswername(“java is a platform”);
  15.     ans2.setPostedBy(“Sudhir Kumar”);
  16.     Answer ans3=new Answer();
  17.     ans3.setAnswername(“Servlet is an Interface”);
  18.     ans3.setPostedBy(“Jai Kumar”);
  19.     Answer ans4=new Answer();
  20.     ans4.setAnswername(“Servlet is an API”);
  21.     ans4.setPostedBy(“Arun”);
  22.     ArrayList<Answer> list1=new ArrayList<Answer>();
  23.     list1.add(ans1);
  24.     list1.add(ans2);
  25.     ArrayList<Answer> list2=new ArrayList<Answer>();
  26.     list2.add(ans3);
  27.     list2.add(ans4);
  28.     Question question1=new Question();
  29.     question1.setQname(“What is Java?”);
  30.     question1.setAnswers(list1);
  31.     Question question2=new Question();
  32.     question2.setQname(“What is Servlet?”);
  33.     question2.setAnswers(list2);
  34.     session.persist(question1);
  35.     session.persist(question2);
  36.     t.commit();
  37.     session.close();
  38.     System.out.println(“success”);
  39. }
  40. }

download this hibernate example

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. Here, we are direct printing the object of answer class, but we have overridden the toString() method in the Answer class returning answername and poster name. So it prints the answer name and postername rather than reference id.

FetchData.java

  1. package com.Kreationnext;
  2. import java.util.*;
  3. import org.hibernate.*;
  4. import org.hibernate.cfg.*;
  5. public class FetchData {
  6. public static void main(String[] args) {
  7.     Session session=new Configuration().configure(“hibernate.cfg.xml”)
  8.                           .buildSessionFactory().openSession();
  9.     Query query=session.createQuery(“from Question”);
  10.     List<Question> list=query.list();
  11.     Iterator<Question> itr=list.iterator();
  12.     while(itr.hasNext()){
  13.         Question q=itr.next();
  14.         System.out.println(“Question Name: “+q.getQname());
  15.         //printing answers
  16.         List<Answer> list2=q.getAnswers();
  17.         Iterator<Answer> itr2=list2.iterator();
  18.         while(itr2.hasNext()){
  19.             System.out.println(itr2.next());
  20.         }
  21.     }
  22.     session.close();
  23.     System.out.println(“success”);
  24. }
  25. }

OUTPUT

listotm3

Comments

comments