This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Difference Between Unidirectional and Bidirectional Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Difference Between Unidirectional and Bidirectional" Watch "Difference Between Unidirectional and Bidirectional" New topic

Difference Between Unidirectional and Bidirectional

i ananthaprakash

Joined: Aug 31, 2011
Posts: 9

I am really confused in understanding difference between Unidirectional and Bidirectional mapping in Hibernate 3.5.
Can some body explain me with an example with tables and also the usage when to use what.

I have setup the hibernate and i have already did some egs i have mapped some <one-to-one> mapping and all.
But Concept wise i am not able to understand.
Thanks in advance.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Unidirectional - means only allow navigating from one side of the mapping to another. For example in the case of a one-many mapping, only allow navigation from the one side to the many side. Bi-directional means to allow navigation both ways.

So much trouble in the world -- Bob Marley
i ananthaprakash

Joined: Aug 31, 2011
Posts: 9
Ok Thanks Bosun..

Now let us consider an example..

Employee -- Class


so 1-to-Many relationship occurred in the above case.
Now i am stuck up in understanding the concept.
When is this mapping considered and go the following questions
1) Is this mapping considered only while fetching the data from database?
2) Is this mapping considered only while storing data to database?
3) Or this is considered for both 1) and 2)
4) Why mapping is used?
5) How to decide which mapping to be used for eg lets consider One-to-Many relationship {Unidirectional or Bidirectional}?

Can you explain a little more with the above example.

Thanks a ton
i ananthaprakash

Joined: Aug 31, 2011
Posts: 9
Can anybody clarify me the above query as it is little urgent.
Thank You in advance.......
sagarpol tambepol

Joined: Mar 02, 2013
Posts: 1
Please Can you more elaborate on Nagivation from from one to another class?
Jayasiva kammana

Joined: Mar 03, 2013
Posts: 1
Dear AnathaPrakash,

As Per Bosun Bello Suggestion the basic understanding of Unidirectional and bidectional is correct. have glance on with sample examples

Let us Consider Class Employee with name ,age and phone as properties.
public class Employee{
private String name;
private int age;
private String phone;
private Department dept;
//getters and setters

we have Department Class with deptId,deptName,deptLocation as properties of the Department Class.

public class Department{
private String deptId;
private String deptName;
private deptLocation;
//getters and setters

In the above Java class configuration defined department object as property of the employee class then we can conclude that there is mapping exist between Employee and department.i.e., Employee is associated department .then above scenario Every employee is mapped to department ,department doesn't know about Employee,because of department doesn't have any association with Employee(i.e.,Department has no Employee). then it unidirection.


Consider Class Person

Class Person{
private String fName;
private String lName;
private int age;
private Address address;
//getters and setters;


Class Address{
private String doorNo;
private String street;
priavate String city;
private String country;

private Person person;


In the above Person class have address and Address can have Person and then we will conclude that that every person has address and each address is mapped to employee so it is birection.

I agree. Here's the link:
subject: Difference Between Unidirectional and Bidirectional
It's not a secret anymore!