Hibernate Many To Many Mapping - Join Tables With Examples

Today we will look into Hibernate Many to Many Mapping using XML and annotation configurations. Earlier we looked how to implement One To One and One To Many mapping in Hibernate.

Hibernate Many to Many

Many-to-Many mapping is usually implemented in database using a Join Table. For example we can have Cart and Item table and Cart_Items table for many-to-many mapping. Every cart can have multiple items and every item can be part of multiple carts, so we have a many to many mapping here.

Hibernate Many to Many Mapping Database Setup

Below script can be used to create our many-to-many example database tables, these scripts are for MySQL database. If you are using any other database, you might need to make small changes to get it working.

Notice that Cart_Items table doesn’t have any extra columns, actually it doesn’t make much sense to have extra columns in many-to-many mapping table. But if you have extra columns, the implementation changes little bit and we will look into that in another post.

Below diagram shows the Entity relationship between these tables.


Our Database setup is ready now, let’s move on to create the hibernate many-to-many mapping project.

Hibernate Many To Many Mapping Project Structure

Create a maven project in Eclipse or your favorite IDE, below image shows the structure and different components in the application.

We will first look into the XML based mapping implementations and then move over to using JPA annotations.

Hibernate Maven Dependencies

Our final pom.xml contains Hibernate dependencies with latest version 4.3.5.Final and mysql driver dependencies.


Hibernate Many to Many XML Configuration Model Classes



Notice that Cart has set of Item and Item has set of Cart, this way we are implementing Bi-Directional associations. It means that we can configure it to save Item when we save Cart and vice versa.

For one-directional mapping, usually we have set in one of the model class. We will use annotations for one-directional mapping.

Hibernate Many To Many Mapping XML Configuration

Let’s create hibernate many to many mapping xml configuration files for Cart and Item. We will implement bi-directional many-to-many mapping.


Notice that set of items is mapped to CART_ITEMS table. Since Cart is the primary object, cart_id is the key and many-to-many mapping is using Item class item_id column.


As you can see from above, the mapping is very similar to Cart mapping configurations.

Hibernate Configuration for XML Based Many to Many Mapping

Our hibernate configuration file looks like below.


Hibernate SessionFactory Utility Class for XML Based Mapping


It’s a simple utility class that works as a factory for SessionFactory.

Hibernate Many To Many Mapping XML Configuration Test Program

Our hibernate many to many mapping setup is ready, let’s test it out. We will write two program, one is to save Cart and see that Item and Cart_Items information is also getting saved. Another one to save item data and check that corresponding Cart and Cart_Items are saved.


When we execute above hibernate many to many mapping example program, we get following output.

Note that once the item data is saved through first cart, the item_id is generated and while saving second cart, it’s not saved again.

Another important point to note is that many-to-many join table data is getting saved when we are committing the transaction. It’s done for better performance incase we choose to rollback the transaction.


Output of above program is:

You can easily relate it to the earlier test program, since we have configured bi-directional mapping we can save Item or Cart and mapped data will get saved automatically.

Hibernate Many To Many Mapping Annotation

Now that we have seen how to configure many-to-many mapping using hibernate xml configurations, let’s see an example of implementing it through annotations. We will implement one-directional many-to-many mapping using JPA annotations.

Hibernate Configuration XML File

Our annotations based hibernate configuration file looks like below.


Hibernate SessionFactory utility class

Our utility class to create SessionFactory looks like below.


Hibernate Many to Many Mapping Annotation Model Classes

This is the most important part for annotation based mapping, let’s first look at the Item table model class and then we will look into Cart table model class.


Item1 class looks simple, there is no relational mapping here.


Most important part here is the use of ManyToMany annotation and JoinTable annotation where we provide table name and columns to be used for many-to-many mapping.

Hibernate Many To Many Annotation Mapping Test Program

Here is a simple test program for our hibernate many to many mapping annotation based configuration.


When we execute above program, it produces following output.

It’s clear that saving cart is also saving data into Item and Cart_Items table. If you will save only item information, you will notice that Cart and Cart_Items data is not getting saved.

That’s all for Hibernate Many-To-Many mapping example tutorial, you can download the sample project from below link and play around with it to learn more.

By admin

Leave a Reply

%d bloggers like this: