在春季启动时与JPA协同工作,并使用Mysql(以intellij为例)。
我尝试保存Game实体时遇到以下错误,但是如果保存Player实体,则一切正常
“通过反射访问持久性[com.raul.daus.Entities.Player#idPlayer]字段[私有java.lang.Integer com.raul.daus.Entities.Player.idPlayer]时出错:1;”
以下是游戏实体:
@Entity
@Data
@Table(name = "games")
public class Game {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_game", nullable = false, insertable = false, updatable = false)
private Integer id_game;
@Column(name = "id_dice", nullable = false)
@OneToMany(targetEntity = Dice.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY,
mappedBy = "id", orphanRemoval = true)
private List<Dice> dices = new ArrayList<Dice>();
@ManyToOne(targetEntity = Player.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "idPlayer")
private Integer idPlayer;
@Column(name = "game_result")
private Integer game_result;
这里的玩家实体
Data
@Entity
@Table(name = "players")
public class Player {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idPlayer", insertable = false, updatable = false, nullable = false)
private Integer idPlayer;
// player name field with 'ANÒNIM' as default
@Column(name = "player_name", nullable = false)
@ColumnDefault("'ANÒNIM'")
private String player_name;
// Timestamp field with the registered date
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "registerDate", nullable = false, updatable = false)
private Date registerDate;
// One to Many relationship with the player games
@OneToMany(targetEntity = Game.class, mappedBy = "idPlayer", fetch = FetchType.LAZY, cascade = CascadeType.ALL,
orphanRemoval = true)
private List<Game> games = new ArrayList<>();
@Column(name = "rate_success", nullable = true, updatable = true)
private double rateSuccess;
谢谢
最佳答案
idPlayer
中的Game
字段映射错误。它应该是:
@Entity
public class Game {
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "idPlayer")
private Player idPlayer;
}