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

     

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 Group.java, Story.java and Group.hbm.xml file in our one-to-many example section.

Story Entity:

In the Entity Story.java 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) {

this.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 ManyToOneRelation.java. Here is the code of  ManyToOneRelation.java 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();
  sess=sessFact.openSession();
  Story story = (Story)sess.load(Story.class, 3);
  Group group = story.getGroup();
  
  System.out.println("Group Name: "+group.getName());

  }
  catch (HibernateException he){
  System.out.println(he.getMessage());
  }
  finally{
  //SessionFactory close 
  sessFact.close();
  //Session close
  sess.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 story0_.id as id3_0_, story0_.info as info3_0_, story0_.parent_id as parent3_3_0_ from story story0_ where story0_.id=?

Hibernate: select group0_.id as id2_0_, group0_.name as name2_0_ from gropuptable group0_ where group0_.id=? 0

Group Name: Java Group 2

 

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

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

 

Download the code example

  2