I am building a spring boot for the first time. I am using Spring boot version 2.3.4. I also use PostgreSQL. I wanted to create two tables: Player and Team. The team should have a list of players.
So I created the next two Entity classes:
@Entity
@Table(name = "Players")
public class Player {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="player_id")
private Long playerId;
private String name;
private int dateOfBirth;
private String originCountry;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "team_id", referencedColumnName = "team_id")
private Team team;
}
and
@Entity
@Table(name = "Teams")
public class Team{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="team_id")
private Long teamId;
private String name;
private String country;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "team", orphanRemoval = true)
private List<Player> players = new ArrayList();
}
As you probably already know, spring boot created the tables automatically. The problem that the Team table doesn't have the players column\s.
What is wrong here? Maybe I don't need to see the players columns in the Team table? (which seems strange to me...) Is there a better way to define a list or array of values?
Also, I want to understand: does @JoinColumn(name="team_id")) defined the column name of Player's team property private Team team;?
Thanks!!