Hibernate One To Many Bidirectional Mapping Example

Hibernate » on Jul 26, 2011 { 5 Comments } By Sivateja

Let us see how to achieve, Bidirectional one to many  mapping in hibernate…

Actually in normal one to many, the relation is from parent to child i mean if we do the operations on parent object will be automatically reflected at child objects too right…?

[ and ]

Similarly in many to one the relation is from child  to parent object, hope you remembered this concept,  if not so just go back and have a look once..

Bidirectional one to many  >>>>  Combination of these above 2

Let us see an example

files required…

  • [pojo]
  • [pojo]
  • Vendor.hbm.xml
  • Customer.hbm.xml
  • hibernate.cfg.xml
  • [Our logic]

package str;

import java.util.Set;

public class Vendor {

	private int vendorId;
	private String vendorName;
	private Set children;

	public int getVendorId() {
		return vendorId;
	public void setVendorId(int vendorId) {
		this.vendorId = vendorId;
	public String getVendorName() {
		return vendorName;
	public void setVendorName(String vendorName) {
		this.vendorName = vendorName;
	public Set getChildren() {
		return children;
	public void setChildren(Set children) {
		this.children = children;


package str;
public class Customer {

	private int customerId;
	private String customerName;
	private int forevenId;
	private Vendor parentObjets;

	public Vendor getParentObjets() {
		return parentObjets;
	public void setParentObjets(Vendor parentObjets) {
		this.parentObjets = parentObjets;
	public int getCustomerId() {
		return customerId;
	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	public String getCustomerName() {
		return customerName;
	public void setCustomerName(String customerName) {
		this.customerName = customerName;

	public int getForevenId() {
		return forevenId;
	public void setForevenId(int forevenId) {
		this.forevenId = forevenId;



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<class name="str.Vendor" table="vendor">

<id name="vendorId" column="vendid"  />
<property name="vendorName" column="vendname" length="10"/>

<set name="children" cascade="all" inverse="true">

<key column="forevenid" />
<one-to-many class="str.Customer" />




  • Here we are writing one new attribute inverse=”true” , means we are intimating to hibernate that we are doing Bi Directional operation
  • But default value of inverse=”false



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<class name="str.Customer" table="customer">

<id name="customerId" column="custid"  />
<property name="customerName" column="custname" length="10"/>
<property name="forevenId" column="forevenid"  insert="false" />

<many-to-one name="parentObjets" column="PrentsIds" cascade="all"/>



<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
<property name="connection.url"></property>
<property name="connection.username">system</property>
<property name="connection.password">admin</property>

<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="">update</property>

<mapping resource="Customer.hbm.xml"></mapping>
<mapping resource="Vendor.hbm.xml"></mapping>

package str;

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;

public class OurLogic {

	public static void main(String args[])

		Configuration cfg = new Configuration();

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();		

		Vendor v =new Vendor();


		 Customer c1=new Customer();


		 Customer c2=new Customer();


         Customer c3=new Customer();


		   // one-to-many
		   Set s=new HashSet();



		   // many-to-one          


		    Transaction tx = session.beginTransaction();



		    System.out.println("One To Many Bi-Directional is Done..!!");



  • See line number 59,60 actually we can save any object either parent or child [ as it is Bi directional inverse will take automatically ], but in our application i saved child object.
  • In this above logic, even though we are saving a single child object, but in the database all child objects are inserted at the time of executing the code, the reason being… the time of saving c1 object, first its parent object v  will be inserted, as the parent object v has 3 child objects so hibernate will save all the 3 child objects in the database
  • In Vendor.hbm.xml, we have included an attribute in the set element called inverse, this attribute informs the hibernate that the relation ship is Bi Directional
  • If we write inverse = “false” then hibernate understands that relationship as unidirectional and generates additional update operations on the database, so in order to reduce the internal operations, we need to include inverse=”true
  • remember, default value of inverse =”false”
  • If we make inverse =”true” the performance will be increased, i guess 🙂



You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


5 Responses to “Hibernate One To Many Bidirectional Mapping Example”
  1. krish says:

    Hi Admin,

    Could you please explain how to handle transactions in hibernate.ASAP.


  2. Kundan Kumar says:

    I have some doubt about this bidirectional mapping. In customer class why we need these two fields.

    private int forevenId;
    private Vendor parentObjets;

    foreevenId and parentObjets represents the same id of vendors class. I mean to say that in customer table column forevenid and PrentsIds will have same primary key of the vendor table. Instead of using these two field we can only use parentObjets in Customer class and we can apply the bidirectional mapping at parentObjects field this way we can come out of database redundancy.

  3. M.S.M.YUSOOF says:

    Hi Sivateja Kandula,

    This website is very useful for us.I like the way you maintain the flow of the program using the same class again and again,and showing that the specific changes by high lighting codes.


    • satyam says:


      when we use one to many mapping that time in child table have foreign key column filled with parent primary key values, but in case of bi-directional mapping foreign key column got blank please explain why.

  4. Nishant says:

    please give database structure also


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.