JPA spring 启动,通过持久反射访问字段 XXXX 时出错 属性
JPA spring boot, Error accessing field XXXX by reflection for persistent property
在 spring 中使用 JPA 启动 Mysql,intellij 为 ide。
我尝试保存游戏实体时遇到以下错误,但如果保存玩家实体,一切正常
"Error accessing field [private java.lang.Integer com.raul.daus.Entities.Player.idPlayer] by reflection for persistent property [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;
谢谢
Game
中的 idPlayer
字段映射错误。应该是:
@Entity
public class Game {
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "idPlayer")
private Player idPlayer;
}
在 spring 中使用 JPA 启动 Mysql,intellij 为 ide。
我尝试保存游戏实体时遇到以下错误,但如果保存玩家实体,一切正常
"Error accessing field [private java.lang.Integer com.raul.daus.Entities.Player.idPlayer] by reflection for persistent property [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;
谢谢
Game
中的 idPlayer
字段映射错误。应该是:
@Entity
public class Game {
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "idPlayer")
private Player idPlayer;
}