Hibernate Mapping

Hibernate mappings are one of the key features of  Hibernate. They establish the relationship between two database tables as attributes in your model. That allows you to easily navigate the associations in your model and Criteria queries.

You can establish either unidirectional or bidirectional i.e you can either model them as an attribute on only one of the associated entities or on both. It will not impact your database mapping tables, but it defines in which direction you can use the relationship in your model and  Criteria queries.

The relationship that can be established between entities are-

  • One To One – It represent one to one relationship between two tables.
  • One To Many/Many To one – It represents the one to many relationship between two tables.
  • Many To Many – It represents thew many to many relationship between two tables.

You all must have heard about these relationship. In this blog, you will learn about all relationship using Hibernate.

One to One

There is One to One relationship between Address. Although Address must not be an Entity as it is a value type but for this Example we will consider Address as a separate Entity.

For One to One relationship, we need to simply annotate the data member of the corresponding class with @OneToOne. After running the application if we look after the table created by the hibernate , we will found that the user_details table has all the field i.e id , userName along with a foreign key of address table column ( In this example ). If we want to acheive two way binding like User object should have Address and viceversa. Then we have to make the User object inside Address class and annotate it with @OneToOne. Then, the address table will also contain the foreign key of user column.

You will see the name of field in both the table will be default depending on table name but to change the default implementation , we have to annotate it with @JoinColumn  with a attribute name.

Address.java

 

One to Many/Many to One

There is One to Many relationship between User and Mobile . As one user can have more than one Mobile . And Many to One relationship between Mobile and User.

For One to Many relationship , we have to annotate in same manner as above with @OneToMany to collection type data memberAfter running the application if we look after the table created by the hibernate , we will found that the hibernate is making a new table for mapping of user_details table and mobile table( In this example ). For two way binding we annotate the user inside the Mobile class with @ManyToOne. 

You will see the mapping table and its column name will have default name generated based on the two tables but to change the default implementation, we will annotate it @JoinTable and the attribute name in this is to change the name of mapping table and attribute joinColumns for the primary key of the same class ( User in this case ) and inverseJoinColumns for the primary key of corresponding class ( Mobile in this case ).

Mobile.java

Many to Many

There is Many to Many relationship between User and Vehicle. We will let consider a world where there is multiple owner of a Vehicle and A owner can also own multiple Vehicle. You can consider that the Vehicle is used for the rent and one User can rent multiple Vehicle and a Vehicle can be rented by multiple Users. 

For Many to Many relationship , we have to annotate in same manner as above with @ManyToMany to collection type data member and for two way binding we have to do the same in the corresponding class. After running the application if we look after the table created by the hibernate , we will found that the there is two mapping table formed one made by first mapping  ( by User in this case ) and other mapping table by Vehicle class. So, In order to restrict it, we have to tell hibernate that the mapping is already done by other class with the help of attribute mappedBy in @ManyToMany. So, we will add this attribute in either of the class User or Vehicle.

Vehicle.java

Now , After looking upto User class you will got to understand things clearly . So here is User class

User.java

 

HibernateMain.java

After executing the main class you will found the table as:

address table

mobile table

user_details table

usr_vehicle table

user_mobile_mapping table

vehicle table

 

You can download the code :     

Leave a Reply