Entity Properties

Player Entity

Player.java
    @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
    }

  1. @Entity designates this POJO as a database table mapping
  2. @Table allows us to specify properties of the table, such as the name, indexes, and unique contraints
  3. 🔑 Primary Key for the database table
  4. 🪪 Will use the database auto-increment feature to populate the id value on insertions
  5. 🪪 We use nullable Long instead of long so that it's easy for Spring to know if any entity has been persisted or not
  6. @Column allows us to customize the column, like it's name, length, nullability, as well as a custom definition
  7. @ManyToOne means that this entity references an entity of another table. The other entity may referene "Many" of this entity. Default fetching is "Eager"
  8. 🤲 @JoinColumn or @JoinColumns specifies the name of the column(s) in this table, and which columns they reference from the other entity. A foreign key constrain will automatically be applied and can be named.

@Entity dictates that this class maps to a database table

Comments