Share on Google+Share on Google+

Hibernate Many-to-one Relationships

This current example demonstrates the use of Many to one relationships.

Hibernate Many-to-one Relationships


Hibernate Many-to-one Relationships - Many to one relationships example using xml meta-data

This current example demonstrates the use of Many  to one relationships. We will use .hbm.xml file to map the relatioships. We will also run the example and load the data from database.

Many-to-One Relationships

A many-to-one relationship is where one entity contains values that refer to another entity (a column or set of columns) that has unique values. In relational databases, these many-to-one relationships are often enforced by foreign key/primary key relationships, and the relationships typically are between fact and dimension tables and between levels in a hierarchy.

In this example multiple stories (Java History, Java Platform, Java News and JEE News) are linked to the Java Group 2 (whose primary key is 1)

Here we will implement Many-to-one relationship between two entries Group and Story.

You can check the, and Group.hbm.xml file in our one-to-many example section.

Story Entity:

In the Entity we have to define Group object and getter and setters for the same:

private Group group;

public Group getGroup() {

return group;


public void setGroup(Group group) { = group;



Explanation of Hibernate mapping:

The following mapping is used to define the many-to-one relationships:

<many-to-one name="group" class="roseindia.Group" column="parent_id" />

To run the example you can execute Here is the code of class:

package roseindia;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ManyToOneRelation {

  public static void main(String[] args) {
  SessionFactory sessFact = null;
  Session sess = null;
  try {
  sessFact=new Configuration().configure().buildSessionFactory();
  Story story = (Story)sess.load(Story.class, 3);
  Group group = story.getGroup();
  System.out.println("Group Name: "+group.getName());

  catch (HibernateException he){
  //SessionFactory close 
  //Session close




In the above code we have loaded the Story entity whose primary key is 3, and then retrieved the parent Group. We have also printed the name of story. If you run the example in Eclipse you should get the following output:

og4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

Hibernate: select as id3_0_, as info3_0_, story0_.parent_id as parent3_3_0_ from story story0_ where

Hibernate: select as id2_0_, as name2_0_ from gropuptable group0_ where

Group Name: Java Group 2


Congratulations you have learned all the types on relations in Hibernate. To recap you have learned:

  1. One-to-one mapping
  2. One-to-many mapping
  3. Many-to-many mapping
  4. Many-to-one mapping


Download the code example




Posted on: January 28, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate Many-to-one Relationships  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image