在创建JPA实体之间的关联时出现错误。

huangapple go评论77阅读模式
英文:

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 = &quot;tbl_players&quot;)
@AttributeOverride(name=&quot;id&quot;, column=@Column(name=&quot;player_id&quot;))
public class Player extends Person implements Serializable{
    private int player_id;
    
    @Column(name = &quot;goals_in_year&quot;)
    private int numberOfGoalsInCurrentYear;
    
    private boolean goalie;
    
    @Column(name = &quot;defended_goals&quot;)
    private int defendedGoals;
    
    public Player(){
        
    }
 }

Manager

@Entity
@Table( name = &quot;tbl_manager&quot;)
@AttributeOverride(name=&quot;id&quot;, column=@Column(name=&quot;manager_id&quot;))
public class Manager extends Person implements Serializable{
    private String dob;
    private int starRating;
    
    @OneToOne
    private Team teamToManage;

    public Manager(){
    }
}

Team

@Entity
@Table( name = &quot;tbl_team&quot;)
public class Team implements Serializable {
    @Column(name = &quot;team_name&quot;)
    @Id
    String teamName;
    
    @OneToOne
    Manager manager;
    
    @Column(name = &quot;team_players&quot;)
    @OneToMany
    private List&lt;Player&gt; mPlayers = new ArrayList&lt;&gt;();
    
    @Column(name = &quot;jersey_color&quot;)
    String jerseyColor;
    
    public Team(){
    }
}

League

@Entity
public class League {
    @Id
    private int league_id;
    
    @OneToMany
    @Column(name = &quot;League Teams&quot;)
    private List&lt;Team&gt; mTeam = new ArrayList&lt;&gt;();
    
    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 = &quot;tbl_players&quot;)
@AttributeOverride(name=&quot;id&quot;, column=@Column(name=&quot;player_id&quot;))
public class Player extends Person implements Serializable{
private int player_id;
@Column(name = &quot;goals_in_year&quot;)
private int numberOfGoalsInCurrentYear;
private boolean goalie;
@Column(name = &quot;defended_goals&quot;)
private int defendedGoals;
@OneToOne // or @OneToMany as you desire
@JoinColumn(name=&quot;team_name&quot;) // here the name you have given to the column in tbl_players
private Team team;
public Player(){
}
}

Manager

@Entity
@Table( name = &quot;tbl_manager&quot;)
@AttributeOverride(name=&quot;id&quot;, column=@Column(name=&quot;manager_id&quot;))
public class Manager extends Person implements Serializable{
private String dob;
private int starRating;
@OneToOne(mappedBy=&quot;manager&quot;)
private Team teamToManage;
public Manager(){
}
}

Team

@Entity
@Table( name = &quot;tbl_team&quot;)
public class Team implements Serializable {
@Id
@Column(name = &quot;team_id&quot;)
private int id;    
@Column(name = &quot;team_name&quot;)
String teamName;
@OneToOne
@JoinColumn(name=&quot;manager_id&quot;)
Manager manager;
@Column(name = &quot;team_players&quot;)
@OneToMany(mappedBy=&quot;team&quot;)
private List&lt;Player&gt; mPlayers = new ArrayList&lt;&gt;();
@Column(name = &quot;jersey_color&quot;)
String jerseyColor;
@ManyToOne(mappedBy=&quot;&quot;)
private League league;
public Team(){
}
}

League

@Entity
@Table( name = &quot;tbl_league&quot;)
public class League {
@Id
@Column(name=&quot;league_id&quot;)
private int league_id;
@OneToMany
@JoinTable(name=&quot;tbl_league_teams&quot;,joinColumns=@JoinColumn(name = &quot;league_id&quot;), inverseJoinColumns=@JoinColumn(name = &quot;team_id&quot;))
private List&lt;Team&gt; mTeam = new ArrayList&lt;&gt;();
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.

huangapple
  • 本文由 发表于 2020年5月2日 14:09:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61555218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定