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
could not get a field value by reflection hibernate
提问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 @JoinColumn
annotation from ratingList
and add mappedBy
attribute:
尝试更改映射注释。删除@JoinColumn
注释ratingList
并添加mappedBy
属性:
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
private List<Rating> ratingList = new ArrayList<Rating>();
where user
is the property name in the Rating
entity that has a @ManyToOne
association with User
.
与 关联user
的Rating
实体中的属性名称在哪里?@ManyToOne
User
回答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.
因此,如果您检查了实体定义,请检查以确保在失败的实际查询中没有像这样的简单错误。这就是我遇到类似错误时的问题。