Hibernate Many-To-Many Unidirectional Mapping Example

The Hibernate Many-To-Many Unidirectional Mapping Example tutorial shows you how to use Hibernate Many-To-Many Unidirectional mapping using annotation based configuration. In Many-To-Many relationship, an intermediate table is added and it is known as Joined table. Its primary key is the combination of primary key of both the associated tables.

We are taking an example of Student and ClassRoom relationship. This relationship said that a student can register multiple ClassRoom and one ClassRoom has many students.

Other interesting posts you may like

Let’s begin.

Create required Database Table
We create two tables with these below scripts:

Here we have first created the main tables STUDENT & CLASS_ROOM. then we have created a joined table STUDENT_CLASS_ROOM whose primary key is the combination of primary keys of STUDENT & CLASS_ROOM.

Create project directory structure

In this Hibernate Many-To-Many Unidirectional Mapping Example , we will create java project with final directory structure:

Hibernate Many-To-Many Unidirectional Mapping Example

Maven dependencies
We provide the required Hibernate and MySQL dependency in pom.xml like this:

Create Model classes

Model class Student and ClassRoom are simple POJO class. Here we are using class Student and ClassRoom with JPA annotations to map them to a database tables (these tables were created in above step).

Student Model class

ClassRoom Model class

Let’s dig deeper:

@ManyToMany says that there is a Many-to-Many relationship between Student and ClassRoom. A Student can enjoy multiple ClassRooms, and a ClassRoom can have multiple Students enjoyed.
The attribute cascade = CascadeType.ALL indicates that while persisting (update or delete) Students, ClassRooms will also be persisted (updated or deleted).

@JoinTable indicates that there is a intermediate table which joins two tables: STUDENT and CLASS_ROOM. This table has a primary key that combines by two primary keys of Student and CLASS_ROOM table. This annotation is mainly used on the owning side of the relationship.

The attribute joinColumns refers to the column name of owning side(ID of STUDENT table), and inverseJoinColumns refers to the column of inverse side of relationship(CLASS_ROOM_ID of CLASS_ROOM table).

Note: Don’t forget override hashcode and equals method which are looked by hibernate when holding entities into collections.

Create Hibernate configuration file

We need to provide for hiberate all stuffs like database dialect, driver class, url and account information to connect database. These stuffs is declared in file hibernate.cfg.xml like this:

This file is placed in src/main/resources folder.

Create Hibernate Utility class

For configuring hibernate on startup and managing session factory we create the HibernateUtil class like this:

Create a demo program
We will create main class to demonstrate this Hibernate Many-To-Many Unidirectional Mapping Example.

In the above code, we have initialized the classRooms property of Student class, and just persisted Student objects. Here, ClassRoom objects will be persisted automatically, this operation is thanks to the Cascade attribute.

Run main class and we get the output like below:

That’s all on the Hibernate Many-To-Many Unidirectional Mapping Example tutorial.

Download complete source code, please hit link below

ManyToManyUniDirectionalMappingExample.zip (34 downloads)

Leave a Comment


Please share it if you found this useful
Hide Buttons