Hibernate One to Many with Collection

In One-to-Many,Many-to-one and Many-to-Many relations an entity contains collections of other entities.A collection ca be primitive values or objects.Hibernate provides following XML elements for the mapping of collection.

  • <array>
  • <list>
  • <set>
  • <bag>
  • <map>

<bag> is used when a list is to be mapped without index.Here I am taking example for <set> element.Here is example for Trainer and Batch.Trainer can have multiple batches.

Entity Class

There are two entities classes here for the relation.Trainer class contains the Set<Batch> for One-to-Many relation.

Trainer.java

package com.startwithjava.beans;

import java.util.Set;
public class Trainer {
 private int id;
 private String name;
 Set<Batch> batches;
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public Set<Batch> getBatches() {
	return batches;
}
public void setBatches(Set<Batch> batches) {
	this.batches = batches;
}
 
}

Batch.java

package com.startwithjava.beans;

public class Batch {
	int id;
	private String slot;
	private String type;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getSlot() {
		return slot;
	}
	public void setSlot(String slot) {
		this.slot = slot;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
}

trainer.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
    <hibernate-mapping>  
       <class name="com.startwithjava.beans.Trainer">  
        <id name="id">  
         <generator class="increment"></generator>  
        </id>   
        <property name="name"></property>    
        <set name="batches" table="batches" cascade="save-update"> 
         <key column="trainerId"/>   
         <one-to-many class="com.startwithjava.beans.Batch"/> 
        </set>          
      </class>  
    </hibernate-mapping>

batch.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping>  
<class name="com.startwithjava.beans.Batch">  
 <id name="id">  
  <generator class="increment"></generator>  
 </id>  
 <property name="slot"></property>
 <property name="type"></property>
 </class>
 </hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "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.MySQLDialect</property>  
         <property name="connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>  
         <property name="connection.username">root</property>  
         <property name="connection.password"></property>  
         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
     <mapping resource="product.hbm.xml"/>  
     <mapping resource="category.hbm.xml"/> 
     <mapping resource="country.hbm.xml"/>  
     <mapping resource="headofstate.hbm.xml"/> 
      <mapping resource="trainer.hbm.xml"/>  
     <mapping resource="batch.hbm.xml"/> 
     </session-factory>  
</hibernate-configuration>  

OneToManyTest.java

package com.startwithjava.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.startwithjava.beans.Batch;
import com.startwithjava.beans.Trainer;

public class OneToManyTest {
	public static void main(String[] args) {  
	    Configuration cfg=new Configuration();  
	    cfg.configure("hibernate.cfg.xml");  
	    SessionFactory sf=cfg.buildSessionFactory();  
	    Session session=sf.openSession();  
	    Transaction tx=session.beginTransaction();
	    Set<Batch> batches = new HashSet<Batch>();
	    Batch batch1 = new Batch();
	    batch1.setSlot("10:30-12:00");
	    batch1.setType("Weekend");
	    
	    Batch batch2 = new Batch();
	    batch2.setSlot("1:30-3:00");
	    batch2.setType("Weekday");
	    
	    batches.add(batch1);
	    batches.add(batch2);
	    
	    Trainer trainer = new Trainer();
	    trainer.setName("Raj");
	    trainer.setBatches(batches);
	    session.save(trainer);
	    tx.commit();
	    session.close();
	}
}

Leave a Reply