Get all updates of Java4s
Follow Us: Email Newsletter

Hibernate One To Many Bidirectional Mapping Example

Hibernate » On Jul 26, 2011 { 3 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]



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


You Might Also Like

  ♦ About Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


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.
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_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
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.