Hibernate Tutorial: Mapping Bag in Collection Mapping - javatportal

Mapping Bag in Collection Mapping (using xml file)

If our persistent class has List object, we can map the List by list or bag element in the mapping file. The bag is just like List but it doesn't require index element.

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

Mapping Bag in hibernate

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

<class name="com.javatportal.Question" table="q100">
          <bag name="answers" table="ans100">
          <key column="qid"></key>
          <element column="answer" type="string"></element>

Example of mapping bag in collection mapping

In this example, we are going to see full example of collection mapping by bag. This is the example of bag if it stores value not entity reference that is why are going to use element instead of one-to-many. If you have seen the example of mapping list, it is same in all cases instead mapping file where we are using bag instead of list.

1. create the Persistent class

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 <String > answers;

//getters and setters



2. create the Mapping file for the persistent class

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"

 <class name="com.javatportal.Question" table="q101">
   <id name="id">
     <generator class="increment"></generator>
   <property name="qname"></property>
   <bag name="answers" table="ans101">
     <key column="qid"></key>
     <element column="answer" type="string"></element>

3. create the configuration file

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"

<!-- Generated by MyEclipse Hibernate Tools.                   -->

        <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"/>


4. Create the class to store the data

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")
	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?");
	Question question2=new Question();
	question2.setQname("What is Servlet?");