Hibernate Many to One Bidirectional Mapping Annotation Example

The Hibernate Many to One Bidirectional Mapping Annotation Example tutorial shows you how to use Hibernate Many-To-One Bidirectional mapping using annotation based configuration. The Many-To-One Bidirectional mapping said that one table has a foreign key column that referring the primary key of associated table.

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

Other interesting posts you may like

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

Noticed that STUDENT table contains a foreign key (here is class_room_id field) referring to a primary key (here is id field) of CLASS_ROOM table.

Create project directory structure

In this Hibernate Many to One Bidirectional Mapping Annotation Example , we will create java project with final directory structure.

Hibernate Many to One Bidirectional Mapping Annotation 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:

@OneToMany on students property says that one ClassRoom can have multiple students. From these students we can navigate from ClassRoom to Student.
The mappedBy attribute indicates that it’s the inverse side of relationship. It means that multiple students are belong to one classRoom.
Thank to the cascade attribute, all Student objects will be persisted/updated/deleted automatically on subsequent persist/update/delete on ClassRoom object.

On Student entity side:

@ManyToOne annotation says that many Students refer to one ClassRoom and the attribute optional=false means this relationship becomes mandatory (no student row can be saved without a classRoom reference).

@JoinColumn annotation indicates that column CLASS_ROOM_ID in Student table will refer to primary key of the CLASS_ROOM table.

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 program to demonstrate
We will create main class to demonstrate this Hibernate Many to One Bidirectional Mapping Annotation Example.

In the above code, you can see that we have updated only classRoom explicitly. But thank to the cascade attribute that is set to all on students property of ClassRoom, so that all student objects will be saved on persisting ClassRoom object.

Run main class and we get the output like below:

That’s it on the Hibernate Many to One Bidirectional Mapping Annotation Example tutorial.

Download complete source code, please hit link below

ManyToOneBidirectionalMappingExample.zip (277 downloads)

7 thoughts on “Hibernate Many to One Bidirectional Mapping Annotation Example”

  1. We are a bunch of volunteers and starting a brand new scheme in our community.
    Your web site provided us with useful information to work on. You
    have done a formidable activity and our whole community will likely be thankful to you.

Leave a Comment


Please share it if you found this useful
Hide Buttons