一尘不染

通过反射休眠无法获取字段值

hibernate

我在JPA中更新对象时遇到问题

我有豆子用户

public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;
    @Column(name = "name", nullable = false)
    private String name;
    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "fk_program_rating")
    private List<Rating> ratingList = new ArrayList<Rating>();
}

public class Rating extends BaseModel {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;
    @ManyToOne()
    @JoinColumn(name = "fk_program_rating", nullable = false)
    @ForeignKey(name = "FK_prog_rate")
    @OnDelete(action = OnDeleteAction.CASCADE)
    private Program program;
}

当尝试更新时给我异常:当表评级包含行时,无法通过反射获得字段值

错误TransactionInterceptor:434-应用程序异常被提交异常com.vodafone.visradio.dataaccess.exception.DataAccessException覆盖:org.hibernate.PropertyAccessException:无法通过com.vodafone.visradio.dataaccess.model.Rating的反射获取器获取字段值。ID

关于这个问题的任何帮助,
谢谢


阅读 286

收藏
2020-06-20

共1个答案

一尘不染

尝试更改映射注释。@JoinColumn从中删除注释ratingList并添加mappedBy属性:

@OneToMany(fetch = FetchType.EAGER, mappedBy = "user") 
private List<Rating> ratingList = new ArrayList<Rating>();

与关联userRating实体中的属性名称在哪里?@ManyToOne``User

2020-06-20