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.
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.
package com.javatportal; import java.util.List; public class Question { private int id; private String qname; private List<Answer> answers; //getters and setters }
The Answer class has its own informations such as id, answername, postedBy etc.
package com.javatportal; public class Answer { private int id; private String answername; private String postedBy; //getters and setters } }
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.
<bag name="answers" cascade="all"> <key column="qid"></key> <one-to-many class="com.javatportal.Answer"/> </list>
In this example, we are going to see full example of mapping list that contains entity reference.
This persistent class defines properties of the class including List.
package com.javatportal; import java.util.List; public class Question { private int id; private String qname; private List<Answer> answers; //getters and setters }
package com.javatportal; public class Answer { private int id; private String answername; private String postedBy; //getters and setters } }
Here, we have created the question.hbm.xml file for defining the list.
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.javatportal.Question" table="q501"> <id name="id"> <generator class="increment"></generator> </id> <property name="qname"></property> <bag name="answers" cascade="all"> <index column="type"></index> <one-to-many class="com.javatportal.Answer"/> </bag> </class> <class name="com.javatportal.Answer" table="ans501"> <id name="id"> <generator class="increment"></generator> </id> <property name="answername"></property> <property name="postedBy"></property> </class> </hibernate-mapping>
This file contains information about the database and mapping file.
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="hbm2ddl.auto">update</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> <property name="connection.username">system</property> <property name="connection.password">oracle</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <mapping resource="question.hbm.xml"/> </session-factory> </hibernate-configuration>
In this class we are storing the data of the question class.
package com.javatportal; import java.util.ArrayList; import org.hibernate.*; import org.hibernate.cfg.*; public class StoreData { public static void main(String[] args) { Session session=new Configuration().configure("hibernate.cfg.xml") .buildSessionFactory().openSession(); Transaction t=session.beginTransaction(); Answer ans1=new Answer(); ans1.setAnswername("java is a programming language"); ans1.setPostedBy("Mithilesh singh"); Answer ans2=new Answer(); ans2.setAnswername("java is a platform"); ans2.setPostedBy("Rahul Kumar"); Answer ans3=new Answer(); ans3.setAnswername("Servlet is an Interface"); ans3.setPostedBy("Neeraj Kumar"); Answer ans4=new Answer(); ans4.setAnswername("Servlet is an API"); ans4.setPostedBy("Mukesh"); ArrayList<Answer> list1=new ArrayList<Answer>(); list1.add(ans1); list1.add(ans2); ArrayList<Answer> list2=new ArrayList<Answer>(); list2.add(ans3); list2.add(ans4); 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"); } }