Hibernate One To Many Bidirectional Mapping Example

Hibernate » On Jul 26, 2011 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…

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





  • 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






  • 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 :-)


Java/J2EE Developer & Web Designer, founder of Java4s.com. Sign-up for the email newsletter for your daily dose of Java tutorials.
Email: sivateja@java4s.com


3 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.



By posting your answer, you agree to our comments policy.
© 2010 - 2016
Java4s - Get It Yourself.
The content & design is copyrighted to Sivateja Kandula, it may not be reproduced on other websites.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs

Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
Contact | About Us | Privacy Policy | Advertise With Us

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