May 7, 2011

Hibernate Entity Mapping: Many-to-one and One-to-many relationship

Map a one-to-many/many-to-one association in the object model. There are 2 entity type classes - Team and Player. A Team object can have many Player objects but a Player object can have a reference to only one Team object. Map, persist and load this association.

Properties of Team class: teamId, teamName, players
Properties of Player class: playerId, playerName, team

1) Create the persistent classes for the Team and Player entities,

package info.icontraining.hibernate;

import java.util.*;

public class Team {

   private Set<Player> players = new HashSet<Player>();
   private String teamName;
   private Long teamId;
   public Set<Player> getPlayers() {
      return players;
   public void setPlayers(Set<Player> players) {
      this.players = players;
   public String getTeamName() {
      return teamName;
   public void setTeamName(String teamName) {
      this.teamName = teamName;
   public Long getTeamId() {
      return teamId;
   public void setTeamId(Long teamId) {
      this.teamId = teamId;

package info.icontraining.hibernate;

public class Player {
   private Team team;
   private Long playerId;
   private String playerName;
   public Team getTeam() {
      return team;
   public void setTeam(Team team) { = team;
   public Long getPlayerId() {
      return playerId;
   public void setPlayerId(Long playerId) {
      this.playerId = playerId;
   public String getPlayerName() {
      return playerName;
   public void setPlayerName(String playerName) {
      this.playerName = playerName;

2) Create the mapping files,


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
   <class name="info.icontraining.hibernate.Team" table="TEAM_TABLE"> 
      <id name="teamId" column="TEAM_ID"> 
         <generator class="native" /> 
      <property name="teamName" column="TEAM_NAME" type="string" />
      <set name="players" inverse="true" cascade="save-update,delete">
        <key column="TEAM_ID" />
        <one-to-many class="info.icontraining.hibernate.Player" />


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
   <class name="info.icontraining.hibernate.Player" table="PLAYER_TABLE"> 
      <id name="playerId" column="PLAYER_ID"> 
         <generator class="native" /> 
      <property name="playerName" column="PLAYER_NAME" type="string" />
      <many-to-one name="team" column="TEAM_ID" class="info.icontraining.hibernate.Team" not-null="true" />  

3) In the hibernate.cfg.xml file, add the following configuration,

<mapping resource="info/icontraining/hibernate/Team.hbm.xml" />
<mapping resource="info/icontraining/hibernate/Player.hbm.xml" />

4) Create the client code in a servlet, as follows,

package info.icontraining.servlets;

import java.util.*; 
import javax.servlet.*; 
import javax.servlet.http.* ; 
import org.hibernate.*; 
import info.icontraining.hibernate.*;

public class TeamPlayer extends HttpServlet { 
   public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{ 
      PrintWriter out = res.getWriter(); 

      Session session = HibernateUtil.getSessionFactory().openSession(); 
      Transaction tx = session.beginTransaction(); 
      Team team = new Team();
      team.setTeamName("My Team");
      Player player1 = new Player();
      Player player2 = new Player();

      Session newSession = HibernateUtil.getSessionFactory().openSession(); 
      Transaction newTransaction = newSession.beginTransaction(); 
      List<Player> players = newSession.createQuery("from Player").list(); 
      out.println( players.size() + " players found:" ); 
      for ( Iterator<Player> iter = players.iterator(); iter.hasNext(); ) { 
         Player player =; 
         out.println( player.getPlayerName() ); 

5) Configure the servlet in the web.xml file,


6) Enter the following URL in the browser to test,


No comments:

Post a Comment