英文:
getting error during the creation of relationship between JPA entities
问题
我试图创建实体,但是我得到了以下错误。
内部异常:异常[EclipseLink-7157](Eclipse持久性服务 - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.ValidationException
异常描述:实体类[class application.Team]必须使用@JoinColumn而不是@Column来映射其关系属性[mPlayers]。
这些是我的实体,我需要使用Java Persistence API(JPA)将数据存储到数据库中。为此,我按照以下方式创建实体。也许我在实体之间创建关系方面做错了。
Person(人物)
@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() {
}
}
Player(选手)
@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(){
}
}
Manager(经理)
@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(){
}
}
Team(团队)
@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(){
}
}
League(联赛)
@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);
}
}
英文:
I'm trying to create entities but I got the following error.
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].
These are my entities, I need to store data into the database using the Java Persistence API (JPA). To do so I create entities as following. Maybe I have created the relationships between entities in the wrong way.
Person
@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() {
}
}
Player
@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(){
}
}
Manager
@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(){
}
}
Team
@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(){
}
}
League
@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);
}
}
答案1
得分: 1
请使用 @JoinColumn 来进行映射(@OneToMany、@ManyToMany、@ManyToOne),而不是 @Column。@Column 用于指定持久化属性或字段的映射列。
英文:
Use @JoinColumn for Mapping (@OneToMany, @ManyToMany, @ManyToOne) instead of @Column. @Column is used to specify the mapped column for a persistent property or field.
答案2
得分: 0
这似乎是因为 @Column(name = "team_players") 和 @OneToMany。请阅读链接 https://stackoverflow.com/a/24206471/11207493。
英文:
It seems to be due to @Column(name = "team_players") and @OneToManyPlease
Please read the link
https://stackoverflow.com/a/24206471/11207493
答案3
得分: 0
为什么不将每个模型类视为一个实体,然后使用ManyToOne或OneToMany来连接这些模型呢?
英文:
Why dont u treat each model class as an entity then u join the model using either ManyToOne or OneToMany
答案4
得分: 0
**球员(Player)**
@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() {
}
}
**经理(Manager)**
@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() {
}
}
**球队(Team)**
@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() {
}
}
**联赛(League)**
@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`,具有 `league_id` 和 `team_id` 列,以便在 `League` 实体中方便使用 `@JoinTable` 注解来映射联赛中的球队。
英文:
Player
@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(){
}
}
Manager
@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(){
}
}
Team
@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(){
}
}
League
@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);
}
}
Create new intermiditate mapping table named tbl_league_teams
with columns league_id
and team_id
to facilitate the @JoinTable
in League
entity to map between teams in a league.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论