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,
Team.java
Player.java
2) Create the mapping files,
Team.hbm.xml
Player.hbm.xml
3) In the hibernate.cfg.xml file, add the following configuration,
4) Create the client code in a servlet, as follows,
5) Configure the servlet in the web.xml file,
6) Enter the following URL in the browser to test,
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,
Team.java
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;
}
}
Player.java
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) {
this.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,
Team.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="info.icontraining.hibernate.Team" table="TEAM_TABLE">
<id name="teamId" column="TEAM_ID">
<generator class="native" />
</id>
<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" />
</set>
</class>
</hibernate-mapping>
Player.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="info.icontraining.hibernate.Player" table="PLAYER_TABLE">
<id name="playerId" column="PLAYER_ID">
<generator class="native" />
</id>
<property name="playerName" column="PLAYER_NAME" type="string" />
<many-to-one name="team" column="TEAM_ID" class="info.icontraining.hibernate.Team" not-null="true" />
</class>
</hibernate-mapping>
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.io.*;
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();
player1.setPlayerName("P1");
player2.setPlayerName("P2");
player1.setTeam(team);
player2.setTeam(team);
team.getPlayers().add(player1);
team.getPlayers().add(player2);
session.save(team);
tx.commit();
session.close();
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 = iter.next();
out.println( player.getPlayerName() );
}
newTransaction.commit();
newSession.close();
}
}
5) Configure the servlet in the web.xml file,
<servlet>
<servlet-name>TeamPlayer</servlet-name>
<servlet-class>info.icontraining.servlets.TeamPlayer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TeamPlayer</servlet-name>
<url-pattern>/teamPlayer</url-pattern>
</servlet-mapping>
6) Enter the following URL in the browser to test,
http://localhost:8080/WebAppName/teamPlayer
No comments:
Post a Comment