Other Tutorials

Hibernate Many To Many Tutorial

Introduction:

In this tutorial, we’ll learn to define and use a many-to-many entity association using Hibernate @ManyToMany annotation.

Context BuildUp:

To follow along with this tutorial, let’s say we have two entities – Employee and Qualification:

ManyToMany Hibernate Relationship

As we know, one employee can multiple qualifications. Also, there can be N number of employees with a specific qualification. It clearly means that the Employee and Qualification entities share a Many-to-Many relationship.

Maven Dependencies:

In our POM, let’s first ensure we have the required dependencies:

We can always find out the latest available version at the Maven Central. Also, we’re using MySQL database for this tutorial.

Database Setup:

To represent a ManyToMany association, we’ll at least need three database tables. So, let’s say we have the below ERD diagram representing our database:

Many To Many ERD Diagram

Also, let’s create our tables:

Creating Entities:

Now that we have our database setup ready, let’s first create the model class – Employee:

A Many-to-Many association has two sides – the owning and the inverse/referencing side. The actual physical mapping to the database is specified on the owning side of the relationship. Here, Employee is the owning entity and so we have used the @JoinTable annotation to define the actual physical database mapping. The @JoinTable annotation defines our employee_qualification join table.

The @JoinColumn annotation specifies the column that’ll be used to join the tables. We have also mentioned that qualification_id would be the inverse join column. It simply means it refers to the inverse side of the relationship which is our Qualification class.

Let’s now define our Qualification entity class:

Since Qualification class is the referencing or the inverse side of the association, we have used the mappedBy attribute to refer to its owning side (Employee).

Also as we can see, we have used @ManyToMany annotation in both of our entities.

Using Many-To-Many Association:

In our main() method, we can test out the above mapping:

On executing the above code, our test data would have been inserted in our employee, qualification and employee_qualification tables.

Conclusion:

In this mini-tutorial, we looked at how to establish a ManyToMany JPA association. We have used JPA annotations over the plain-old XML configurations as they are more convenient to use and are becoming increasingly popular.

One comment
Pingback: Hibernate Many To Many Tutorial | Hey Android - Android World

Leave a Comment

Your email address will not be published. Required fields are marked *