在创建 JPA 实体之间的关系时出错
getting error during the creation of relationship between JPA entities
我正在尝试创建实体,但出现以下错误。
Internal Exception: Exception [EclipseLink-7157] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Entity class [class application.Team] must use a @JoinColumn instead of @Column to map its relationship attribute [mPlayers].
这些是我的实体,我需要使用 Java Persistence API (JPA) 将数据存储到数据库中。为此,我创建了如下实体。也许我以错误的方式创建了实体之间的关系。
人
@MappedSuperclass
public class Person {
@Id
@GeneratedValue( strategy= GenerationType.AUTO )
protected int p_id;
protected String firstName;
protected String middleName;
protected String lastName;
protected String phone;
protected String email;
public Person() {
}
}
玩家
@Entity
@Table( name = "tbl_players")
@AttributeOverride(name="id", column=@Column(name="player_id"))
public class Player extends Person implements Serializable{
private int player_id;
@Column(name = "goals_in_year")
private int numberOfGoalsInCurrentYear;
private boolean goalie;
@Column(name = "defended_goals")
private int defendedGoals;
public Player(){
}
}
经理
@Entity
@Table( name = "tbl_manager")
@AttributeOverride(name="id", column=@Column(name="manager_id"))
public class Manager extends Person implements Serializable{
private String dob;
private int starRating;
@OneToOne
private Team teamToManage;
public Manager(){
}
}
团队
@Entity
@Table( name = "tbl_team")
public class Team implements Serializable {
@Column(name = "team_name")
@Id
String teamName;
@OneToOne
Manager manager;
@Column(name = "team_players")
@OneToMany
private List<Player> mPlayers = new ArrayList<>();
@Column(name = "jersey_color")
String jerseyColor;
public Team(){
}
}
联赛
@Entity
public class League {
@Id
private int league_id;
@OneToMany
@Column(name = "League Teams")
private List<Team> mTeam = new ArrayList<>();
public void addTeam(Team team) {
mTeam.add(team);
}
public void removeTeam(Team team) {
mTeam.remove(team);
}
}
这似乎是由于@Column(name = "team_players") 和@OneToManyPlease
请阅读link
使用@JoinColumn 代替@Column 进行映射(@OneToMany、@ManyToMany、@ManyToOne)。 @Column 用于指定持久化属性 或字段的映射列。
为什么不将每个模型 class 视为一个实体,然后使用 ManyToOne 或 OneToMany
加入模型
玩家
@Entity
@Table( name = "tbl_players")
@AttributeOverride(name="id", column=@Column(name="player_id"))
public class Player extends Person implements Serializable{
private int player_id;
@Column(name = "goals_in_year")
private int numberOfGoalsInCurrentYear;
private boolean goalie;
@Column(name = "defended_goals")
private int defendedGoals;
@OneToOne // or @OneToMany as you desire
@JoinColumn(name="team_name") // here the name you have given to the column in tbl_players
private Team team;
public Player(){
}
}
经理
@Entity
@Table( name = "tbl_manager")
@AttributeOverride(name="id", column=@Column(name="manager_id"))
public class Manager extends Person implements Serializable{
private String dob;
private int starRating;
@OneToOne(mappedBy="manager")
private Team teamToManage;
public Manager(){
}
}
团队
@Entity
@Table( name = "tbl_team")
public class Team implements Serializable {
@Id
@Column(name = "team_id")
private int id;
@Column(name = "team_name")
String teamName;
@OneToOne
@JoinColumn(name="manager_id")
Manager manager;
@Column(name = "team_players")
@OneToMany(mappedBy="team")
private List<Player> mPlayers = new ArrayList<>();
@Column(name = "jersey_color")
String jerseyColor;
@ManyToOne(mappedBy="")
private League league;
public Team(){
}
}
联赛
@Entity
@Table( name = "tbl_league")
public class League {
@Id
@Column(name="league_id")
private int league_id;
@OneToMany
@JoinTable(name="tbl_league_teams",joinColumns=@JoinColumn(name = "league_id"), inverseJoinColumns=@JoinColumn(name = "team_id"))
private List<Team> mTeam = new ArrayList<>();
public void addTeam(Team team) {
mTeam.add(team);
}
public void removeTeam(Team team) {
mTeam.remove(team);
}
}
创建名为 tbl_league_teams
的新中间映射 table,包含列 league_id
和 team_id
以便于映射 League
实体中的 @JoinTable
联赛中的球队之间。
我正在尝试创建实体,但出现以下错误。
Internal Exception: Exception [EclipseLink-7157] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Entity class [class application.Team] must use a @JoinColumn instead of @Column to map its relationship attribute [mPlayers].
这些是我的实体,我需要使用 Java Persistence API (JPA) 将数据存储到数据库中。为此,我创建了如下实体。也许我以错误的方式创建了实体之间的关系。
人
@MappedSuperclass
public class Person {
@Id
@GeneratedValue( strategy= GenerationType.AUTO )
protected int p_id;
protected String firstName;
protected String middleName;
protected String lastName;
protected String phone;
protected String email;
public Person() {
}
}
玩家
@Entity
@Table( name = "tbl_players")
@AttributeOverride(name="id", column=@Column(name="player_id"))
public class Player extends Person implements Serializable{
private int player_id;
@Column(name = "goals_in_year")
private int numberOfGoalsInCurrentYear;
private boolean goalie;
@Column(name = "defended_goals")
private int defendedGoals;
public Player(){
}
}
经理
@Entity
@Table( name = "tbl_manager")
@AttributeOverride(name="id", column=@Column(name="manager_id"))
public class Manager extends Person implements Serializable{
private String dob;
private int starRating;
@OneToOne
private Team teamToManage;
public Manager(){
}
}
团队
@Entity
@Table( name = "tbl_team")
public class Team implements Serializable {
@Column(name = "team_name")
@Id
String teamName;
@OneToOne
Manager manager;
@Column(name = "team_players")
@OneToMany
private List<Player> mPlayers = new ArrayList<>();
@Column(name = "jersey_color")
String jerseyColor;
public Team(){
}
}
联赛
@Entity
public class League {
@Id
private int league_id;
@OneToMany
@Column(name = "League Teams")
private List<Team> mTeam = new ArrayList<>();
public void addTeam(Team team) {
mTeam.add(team);
}
public void removeTeam(Team team) {
mTeam.remove(team);
}
}
这似乎是由于@Column(name = "team_players") 和@OneToManyPlease
请阅读link
使用@JoinColumn 代替@Column 进行映射(@OneToMany、@ManyToMany、@ManyToOne)。 @Column 用于指定持久化属性 或字段的映射列。
为什么不将每个模型 class 视为一个实体,然后使用 ManyToOne 或 OneToMany
加入模型玩家
@Entity
@Table( name = "tbl_players")
@AttributeOverride(name="id", column=@Column(name="player_id"))
public class Player extends Person implements Serializable{
private int player_id;
@Column(name = "goals_in_year")
private int numberOfGoalsInCurrentYear;
private boolean goalie;
@Column(name = "defended_goals")
private int defendedGoals;
@OneToOne // or @OneToMany as you desire
@JoinColumn(name="team_name") // here the name you have given to the column in tbl_players
private Team team;
public Player(){
}
}
经理
@Entity
@Table( name = "tbl_manager")
@AttributeOverride(name="id", column=@Column(name="manager_id"))
public class Manager extends Person implements Serializable{
private String dob;
private int starRating;
@OneToOne(mappedBy="manager")
private Team teamToManage;
public Manager(){
}
}
团队
@Entity
@Table( name = "tbl_team")
public class Team implements Serializable {
@Id
@Column(name = "team_id")
private int id;
@Column(name = "team_name")
String teamName;
@OneToOne
@JoinColumn(name="manager_id")
Manager manager;
@Column(name = "team_players")
@OneToMany(mappedBy="team")
private List<Player> mPlayers = new ArrayList<>();
@Column(name = "jersey_color")
String jerseyColor;
@ManyToOne(mappedBy="")
private League league;
public Team(){
}
}
联赛
@Entity
@Table( name = "tbl_league")
public class League {
@Id
@Column(name="league_id")
private int league_id;
@OneToMany
@JoinTable(name="tbl_league_teams",joinColumns=@JoinColumn(name = "league_id"), inverseJoinColumns=@JoinColumn(name = "team_id"))
private List<Team> mTeam = new ArrayList<>();
public void addTeam(Team team) {
mTeam.add(team);
}
public void removeTeam(Team team) {
mTeam.remove(team);
}
}
创建名为 tbl_league_teams
的新中间映射 table,包含列 league_id
和 team_id
以便于映射 League
实体中的 @JoinTable
联赛中的球队之间。