Today we will look into One To Many Mapping in Hibernate. We will look into Hibernate One To Many Mapping example using Annotation and XML configuration.

One To Many Mapping in Hibernate

In simple terms, one to many mapping means that one row in a table can be mapped to multiple rows in another table. For example, think of a Cart system where we have another table for Items. A cart can have multiple items, so here we have one to many mapping. We will use Cart-Items scenario for our hibernate one to many mapping example.

One To Many Mapping in Hibernate – Database Setup

We can use foreign key constraint for one to many mapping. Below is our database script for Cart and Items table. I am using MySQL database for Hibernate one to many mapping example.

setup.sql

Below is the ER diagram of the Cart and Items table.

Our database setup is ready, let’s move on to creating hibernate One to Many Mapping example project. First of all, we will use XML based configuration and then we will implement one to many mapping using Hibernate and JPA annotation.

Hibernate One To Many Mapping Project Structure

Create a simple Maven project in Eclipse or you favorite IDE, the final project structure will look like below image.

Hibernate-One-To-Many-Mapping-Project1-450x277

Hibernate Maven Dependencies

Our final pom.xml file contains dependencies for Hibernate and MySQL driver. Hibernate uses JBoss logging and it automatically gets added as transitive dependencies.

Note that I am using latest Hibernate version 4.3.5.Final and MySQL driver version based on my database installation.

Hibernate One To Many Mapping Model Classes

For our tables Cart and Items, we have model classes to reflect them.

Cart.java

I am using Set of Items, so that every record is unique. We can also use List or Array for one to many mapping in hibernate.

Items.java

Items have many to one relationship to Cart, so we don’t need to have Collection for Cart object.

Hibernate SessionFactory Utility Class

We have a utility class for creating Hibernate SessionFactory.

HibernateUtil.java

Hibernate Configuration XML File

Our hibernate configuration xml file contains database information and mapping resource details.

hibernate.cfg.xml

Hibernate One To Many Mapping Example – XML Configuration

This is the most important part of tutorial, let’s see how we have to map both Cart and Items classes for one to many mapping in hibernate.

cart.hbm.xml

The important part is the set element and one-to-many element inside it. Notice that we are providing key to be used for one to many mapping i.e cart_id.

items.hbm.xml

Notice that from items to cart, it’s many to one relationship. So we need to use many-to-one element for cart and we are providing column name that will be mapped with the key. So based on the Cart hibernate mapping configuration, it’s key cart_id will be used for mapping.

Our project for Hibernate One To Many Mapping Example using XML mapping is ready, let’s write a test program and check if it’s working fine or not.

Hibernate One To Many Mapping Example – Test Program

HibernateOneToManyMain.java

Notice that we need to save both Cart and Items objects one by one. Hibernate will take care of updating the foreign keys in Items table. When we execute above program, we get following output.

Notice that Hibernate is using Update query to set the cart_id in ITEMS table.

Hibernate One To Many Mapping Annotation

Now that we have seen how to implement One To Many mapping in Hibernate using XML based configurations, let’s see how we can do the same thing using JPA annotations.

Hibernate One To Many Mapping Example Annotation

Hibernate configuration file is almost same, except that mapping element changes because we are using Classes for hibernate one to many mapping using annotation.

hibernate-annotation.cfg.xml

Hibernate SessionFactory Utility Class

SessionFactory utility class is almost same, we just need to use the new hibernate configuration file.

HibernateAnnotationUtil.java

Hibernate One To Many Mapping Annotation Model Classes

Since we don’t have xml based mapping files, all the mapping related configurations will be done using JPA annotations in the model classes. If you understand the xml based mapping, it’s very simple and similar.

Cart1.java

Important point to note is the OneToMany annotation where mappedBy variable is used to define the property in Items1 class that will be used for the mapping purpose. So we should have a property named “cart1” in Items1 class. Don’t forget to include all the getter-setter methods.

Items1.java

Most important point in above class is the ManyToOne annotation on Cart1 class variable and JoinColumn annotation to provide the column name for mapping.

That’s it for one to many mapping in hibernate using annotation in model classes. Compare it with XML based configurations, you will find them very similar.

Let’s write a test program and execute it.

Hibernate One To Many Mapping Annotation Example Test Program

Our test program is just like xml based configuration, we are just using the new classes for getting Hibernate Session and saving the model objects into database.

HibernateOneToManyAnnotationMain.java

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

That’s all for hibernate one to many mapping, download the sample project from below link and do some more experiments.

By admin

Leave a Reply

%d bloggers like this: