当前位置: 首页 > 知识库问答 >
问题:

JPA Spring Boot,通过持久属性的反射访问字段XXXX时出错

傅穆冉
2023-03-14

用Mysql,intellij作为IDE在Spring启动时使用JPA。

当我尝试保存游戏实体时,我会遇到以下错误,但如果保存玩家实体,则所有操作都很正常

@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;

谢谢

共有1个答案

颜瀚漠
2023-03-14

game中的idplayer字段映射错误。应该是:

java prettyprint-override">@Entity
public class Game {

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "idPlayer")
    private Player idPlayer;

}
 类似资料: