Hibernate Mapping Bag Example


Hibernate Mapping Bag Example

Hibernate Mapping Bag Example, here a <bag> is an unordered collection, which can contain duplicated elements. It means if you persist a bag with some order of elements, you can not expect the same order retains when the collection is retrieved.

A <set> is similar to <bag> except <bag> can store duplicate objects.

When to Use <bag>

If we want to insert child record of parent record along with their index then we can use <list> tag, if do not want to maintain index then we can go for <bag> tag in hibernate mapping.

Hibernate Mapping Bag Example

  1. Student
  2. Course
  3. Student.hbm.xml
  4. Course.hbm.xml
  5. hibernate.cfg.xml
  6. Run as main class

Student

package com.javagf.bean;

import java.util.Set;
public class Student {
  private int roll;
  private String name;
  List<Course> child;
  
  // setters and getters
}

Course

package com.javagf.bean;
public class Course {
    private int courseId;
    private String courseName;
    private int courseDuration;
    //setters and getters
}

Student.hbm.xml

<?xml version="1.0"?>
<!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.javagf.bean.Student" table="STUDENT">
       <id name="roll" type="int"></id>
       <property name="name" type="java.lang.String"></property>
       <property name="address" type="java.lang.String"></property>
       <bag name="child" table="Course" cascade="all">
          <key column="courseId" ></key>
          <one-to-many class="com.javagf.bean.Course" />
       </bag>
    </class>
</hibernate-mapping>

Course.hbm.xml

<?xml version="1.0"?>
<!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.javagf.bean.Course" table="COURSE"> 
       <id name="courseId" type="int"></id>
       <property name="courseName" type="java.lang.String"></property>
       <property name="courseDuration" type="int"></property> 
   </class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<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">manager</property> 
     <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
     <property name="show_sql">true</property>
     <mapping resource="com/javagf/bean/Student.hbm.xml" />  
     <mapping resource="com/javagf/bean/Fee.hbm.xml" />
 </session-factory>
</hibernate-configuration>

Run

package com.javagf.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.javagf.bean.Fee;
import com.javagf.bean.Student;

public class Run {
 public static void main(String[] args) {
   Configuration cfg = new Configuration(); 
   cfg.configure("com/javagf/configuration/hibernate.cfg.xml");
   SessionFactory factory = cfg.buildSessionFactory();
   Session session = factory.openSession();
   Transaction transaction = session.beginTransaction();
  
   Student student = new Student();
   student.setRoll(4001);
   student.setName("Rakhi");
   student.setAddress("Hi-Tech");
   
   Course c1 = new Course();
   c1.setCourseId(4001);
   c1.setCourseDuration(6);
   c1.setCourseName("Java");
  
   List<Course> list = new ArrayList<>();
   list.add(c1);
   // Add c1 object to student 
   student.setChild(list);
   session.save(student); 
   transaction.commit();
   session.close();
 }
}

Related Posts

  1. Hibernate mapping tutorial
  2. Hibernate many to one mapping example
  3. Hibernate Many to Many Mapping Example
  4. Hibernate one to one mapping example
  5. Mapping List in Collection Mapping






Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229