jpa spring引导,通过反射访问字段x以获取持久属性时出错

8hhllhi2  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(436)

使用mysql和intellij作为ide在spring boot中使用jpa。
当我试图保存游戏实体时,我面临以下错误,但是如果保存一个玩家实体,一切都正常
“通过永久属性[com.raul.daus.entities.player]的反射访问字段[private java.lang.integer com.raul.daus.entities.player]时出错: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;

谢谢

0aydgbwb

0aydgbwb1#

这个 idPlayer 字段输入 Game Map错误。应该是:

@Entity
public class Game {

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

}

相关问题