Hibernate Bi-Directional Mapping in Detail - Hibernate
Home » Hibernate, Tech Notes

Hibernate Bi-Directional Mapping in Detail

12 March 2009 One Comment

Bidirectional mapping is the most common mapping. This is the typical parent child relationship scenario where the children can be retrieved using parent, and the parent can be retrieved using a child. In short, these relations can be traverse in either direction. Hibernate achieves this using a property ‘inverse=true/false’. This property can be applied in all mapping possibilities i.e. Many-to-one, one-to-many and one-to-one. Sometimes, it will be as simple as adding this property to the link element. Let us take an example of this mapping. We continue with same customer and address example with Many – to – one in parent to child direction, and one-to-many in reverse direction.

Database:
Database remains same as the Unidirectional many-to-one example.

TAB_CUSTOMER
	CUSTOMER_ID - Number - Not NULL Primary Key
	ADDRESS_ID - Number - Not NULL Foreign Key from Tab_Address table

TAB_ADDRESS
	ADDRESS_ID - Number - Not NULL Primary Key

Hibernate Mapping:
Let us reflect this bidirectional business in our mapping.


  



  

	
	


Java:
Here, we will have Customer class containing an attribute to hold address; also Address class will hold customers. Simple reason, if you retrieve customer object, then also you will have complete information of addresses of that customer. Even if you start data retrieval using address object, you will get information of customers which have this address.

public class Customer {
    private Long id;
    private Address address;
    // getter/setter for id & address fields
}     

  public class Address {
          private Long id;
         private Set customers;
 // getter/setter for id and customers fields
}

Extension to One-to-One Relation:
If we want to extend the bidirectional relation to one-to-one association, then it is easier than above example. Instead of multiple objects/collection of customers in Address class, we will have only one object. You will definitely need the unique constraint on the foreign key column.

Extension to Join table:
Just to give brief introduction of this mapping style, here we link the two classes using ‘join’ tag. Take a look at the many to many unidirectional association example. We used an intermediate table to link customer and address entities. Now we join our customer table to the intermediate customeraddress table, and also address table to the customeraddress table. Same thing reflected in the mapping and inverse can be added on any side of the association. The mapping of this scenario will be like following.


  

	
	



  

	
	


Just to add, in both Java classes, we need attributes to hold instance of the other class.

 

More Related Posts in Hibernate, Tech Notes

One Comment »

  • anil said:

    Good one . Finally understood !!! Thanks

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.