The roster Application
The
roster
application maintains the team rosters for players in recreational sports leagues. The application has four components: Java Persistence API entities (Player
,Team
, andLeague
), a stateful session bean (RequestBean
), an application client (RosterClient
), and three helper classes (PlayerDetails
,TeamDetails
, andLeagueDetails
).Functionally,
roster
is similar to theorder
application described earlier in this chapter with two new features thatorder
does not have: many-to-many relationships and automatic table creation at deploytime.Relationships in the roster Application
A recreational sports system has the following relationships:
In
roster
this is reflected by the following relationships between thePlayer
,Team
, andLeague
entities:The Many-To-Many Relationship in roster
The many-to-many relationship between
Player
andTeam
is specified by using the@ManyToMany
annotation.In
Team.java
, the@ManyToMany
annotation decorates thegetPlayers
method:@ManyToMany(cascade=REMOVE) @JoinTable( name="EJB_ROSTER_TEAM_PLAYER", joinColumns= @JoinColumn(name="TEAM_ID", referencedColumnName="ID"), inverseJoinColumns= @JoinColumn(name="PLAYER_ID", referencedColumnName="ID") ) public Collection<Player> getPlayers() { return players; }The
@JoinTable
annotation is used to specify a table in the database that will associate player IDs with team IDs. The entity that specifies the@JoinTable
is the owner of the relationship, so in this case theTeam
entity is the owner of the relationship with thePlayer
entity. Becauseroster
uses automatic table creation at deploytime, the container will create a join table in the database namedEJB_ROSTER_TEAM_PLAYER
.
Player
is the inverse, or non-owning side of the relationship withTeam
. As one-to-one and many-to-one relationships, the non-owning side is marked by themappedBy
element in the relationship annotation. Because the relationship betweenPlayer
andTeam
is bidirectional, the choice of which entity is the owner of the relationship is arbitrary.In
Player.java
, the@ManyToMany
annotation decorates thegetTeams
method:@ManyToMany(cascade=REMOVE, mappedBy="players") public Collection<Team> getTeams() { return teams; }Automatic Table Generation in roster
At deploytime the Application Server will automatically drop and create the database tables used by
roster
. This is done by setting thetoplink.ddl-generation
property todrop-and-create-tables
inpersistence.xml
.<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="em" transaction-type="JTA"> <jta-data-source>jdbc/__default</jta-data-source> <properties> <property name="toplink.ddl-generation" value="drop-and-create-tables"/> </properties> </persistence-unit> </persistence>This feature is specific to the Java Persistence API provider used by the Application Server, and is non-portable across Java EE servers. Automatic table creation is useful for development purposes, however, and the toplink.ddl-generation property may be removed from
persistence.xml
when preparing the application for production use, or when deploying to other Java EE servers.