Can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Hibernate One To Many Bidirectional Mapping Example

Hibernate » On Jul 26, 2011 | { 2 Comments }

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]

Vendor.java

Customer.java

Vendor.hbm.xml

Note:

  • 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

 

Customer.hbm.xml

hibernate.cfg.xml

OurLogic.java

Notes:

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

 

 

What you are thinkig....

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

    Regards
    krish

  2. Kundan Kumar says:

    Hi,
    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.

If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum

Name*
Ask a Question ?
or
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
Jsps,Web Services
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.