@Entity // (1)
@Table(name = "lu_player", // (2)
indexes = {
@Index(columnList = "firstName, lastName", name = "idx_lu_player_first_name"),
@Index(columnList = "lastName, firstName", name = "idx_lu_player_last_name"),
@Index(columnList = "position_id", name = "idx_lu_player_position"),
@Index(columnList = "team_id", name = "idx_lu_player_team"),
}
)
public class Player {
@Id // (3)
@GeneratedValue(strategy = GenerationType.IDENTITY) // (4)
private Long id; // (5)
@Column(length = 50) // (6)
private String firstName;
@Column(length = 50)
private String lastName;
@ManyToOne(fetch = FetchType.EAGER) // (7)
@JoinColumn(name = "position_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "FK_PLAYER_TO_POSITION")) // (8)
private Position position;
@ManyToOne
@JoinColumn(name = "team_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "FK_PLAYER_TO_TEAM"))
@JsonIgnoreProperties("roster")
private Team team;
@OneToMany(mappedBy = "player"
, cascade = {CascadeType.PERSIST, CascadeType.MERGE}
)
// Ensure at the database level that if a player is deleted, all of their awards are also deleted
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnoreProperties("player")
private List<AwardReceived> receivedAwards;
@ManyToMany(
cascade = {CascadeType.PERSIST, CascadeType.MERGE}
)
@JoinTable(
name = "j_player_team",
joinColumns = @JoinColumn(name = "player_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "FK_PLAYER_TEAM_TO_PLAYER")),
inverseJoinColumns = @JoinColumn(name = "team_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "FK_PLAYER_TEAM_TO_TEAM"))
)
// Spring knows to delete the join table records when a player is deleted
// However, without an OnDelete Cascade, you cannot delete a player from the database because you will get a FK constrain violation
@OnDelete(action = OnDeleteAction.CASCADE)
private List<Team> teamsPlayedFor;
private Boolean franchiseTagged;
@Transient
@JsonIgnore
private String someTempVariable;
// Getters and Setters
}