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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19763559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:01:08  来源:igfitidea点击:

could not get a field value by reflection hibernate

javahibernatejpa

提问by abdelhady

I have problem when update object in jpa

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

i have bean user

我有豆用户

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>();
}

and

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

when try to update that give me exception : could not get a field value by reflection that happen when table rating have rows

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

ERROR TransactionInterceptor:434 - Application exception overridden by commit exception com.vodafone.visradio.dataaccess.exception.DataAccessException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.vodafone.visradio.dataaccess.model.Rating.id

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

any help about this problem
thanks

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

采纳答案by Debojit Saikia

Try changing the mapping annotations. Remove the @JoinColumnannotation from ratingListand add mappedByattribute:

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

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

where useris the property name in the Ratingentity that has a @ManyToOneassociation with User.

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

回答by Jon

Another thing that can cause this is if you don't quite do your criteria correctly.

可能导致这种情况的另一件事是,如果您没有完全正确地执行您的标准。

For example if you put "rating"in the criteria, but you actually need "rating.id". That will cause this same error message to come up.

例如,如果您"rating"输入了条件,但实际上需要"rating.id". 这将导致出现相同的错误消息。

So if you've checked your Entity definitions check to make sure you don't have a simply mistake like this in the actual query that is failing. That was the issue when I was getting a similar error.

因此,如果您检查了实体定义,请检查以确保在失败的实际查询中没有像这样的简单错误。这就是我遇到类似错误时的问题。