Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bi directional relationship

 
Joybrata Chakraborty
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I like to understand when we will use bi directional relationship in JPA. I have table A & B. B is having the foreign key of A. In that case whether I need to use bi directional relationship between A & B entity.

Thanks
Joy
 
Vijitha Kumara
Bartender
Pie
Posts: 3930
21
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on whether it makes sense to have the bi-directionality in the particular domain you work with.
 
Ran Pleasant
Ranch Hand
Posts: 76
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joybrata

In determining "when to use bi-directional relationships" it is important to look at the sturcture of your data. A bi-directional relationship is not a big issues with simple parent/child objects, such as an invoice, since they form a complete unit of work. In other words, reading an invoice will (should) never pull more than the invoice and its lineitems.

A bi-directional relationship is a very big issues with complex parent/child objects, such as accounts, since they do not form complete units of work. Consider that an account may have several child accounts, which in turn may have their own child accounts, etc., etc., If your Account object has bi-directional relationship with its parent then when you read an account you will not only get it's children and their children, etc., you will also get the account's parent and its children and their children, etc., etc. Without realizing it you could be pulling a BIG set of data that requires the firing of many queries. Reading thousands of objects from the database and sending them across your network just to use a couple of the objects will be really hard to justify to your managers, DBAs, network admins, etc.

In short I would suggest never using bi-relationships unless: (1) You know that you are dealing with a complete unit of work and (2) you have a real need for the relationship.

Also don't make any assumptions about the amount of data and the distribution of child objects under parent objects. Even a simple child-parent relationship can cause major problems. I once created an Account object that contain child accounts. It worked great in testing but instancely blew up in production because the client had several hundred throusand accounts and 95% of them shared the same parent. I removed the relationship, read objects as needed, and everything worked fine.
 
Joybrata Chakraborty
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ran for explaining the thing to me. Its very well written.
Joy
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ran, that is great explanation. It gave me good insight about bi-directional relationship. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic