Reputation: 3
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);
}
}
Upvotes: 0
Views: 137
Reputation: 1099
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.
Upvotes: 0
Reputation: 111
Why dont u treat each model class as an entity then u join the model using either ManyToOne or OneToMany
Upvotes: 0
Reputation: 424
Use @JoinColumn for Mapping (@OneToMany, @ManyToMany, @ManyToOne) instead of @Column. @Column is used to specify the mapped column for a persistent property or field.
Upvotes: 1
Reputation: 153
It seems to be due to @Column(name = "team_players") and @OneToManyPlease
Please read the link https://stackoverflow.com/a/24206471/11207493
Upvotes: 0