background image

Entity Inheritance in the roster Application

<< The roster Application | SummerLeague Class >>
<< The roster Application | SummerLeague Class >>

Entity Inheritance in the roster Application

@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 the Team entity is the owner of the relationship with the Player entity. Because
roster
uses automatic table creation at deploytime, the container will create a join table in the
database named EJB_ROSTER_TEAM_PLAYER.
Player
is the inverse, or non-owning side of the relationship with Team. As one-to-one and
many-to-one relationships, the non-owning side is marked by the mappedBy element in the
relationship annotation. Because the relationship between Player and Team is bidirectional, the
choice of which entity is the owner of the relationship is arbitrary.
In Player.java, the @ManyToMany annotation decorates the getTeams method:
@ManyToMany(mappedBy=
"players")
public Collection<Team> getTeams() {
return teams;
}
Entity Inheritance in the roster Application
The roster application demonstrates how to use entity inheritance, as described in
"Entity
Inheritance" on page 691
.
The League entity in roster is an abstract entity with two concrete subclasses: SummerLeague
and WinterLeague. Because League is an abstract class it cannot be instantiated:
...
@Entity
@Table(name =
"EJB_ROSTER_LEAGUE")
public abstract class League implements java.io.Serializable {
...
}
Instead, SummerLeague or WinterLeague are used by clients when creating a league.
SummerLeague
and WinterLeague inherit the persistent properties defined in League, and only
add a constructor that verifies that the sport parameter matches the type of sport allowed in that
seasonal league. For example, here is the SummerLeague entity:
...
@Entity
public class SummerLeague extends League
The roster Application
Chapter 26 · Persistence in the EJB Tier
727