无法将 java.lang.Integer 字段设置为 java.lang.Integer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24693853/
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
Can not set java.lang.Integer field to java.lang.Integer
提问by gstackoverflow
User declaration:
用户声明:
@Entity
public class User {
@Id
@GeneratedValue
private Integer id;
....
Pattern declaration:
模式声明:
@Entity
public class Pattern {
@Id
@GeneratedValue
Integer id;
...
UserPatternDeclaration:
用户模式声明:
public class UserPattern {
@Id
@GeneratedValue
Integer id;
@ManyToOne
@JoinColumn(name = "user_id")
User user;
@ManyToOne
@JoinColumn(name = "pattern_id")
Pattern pattern;
...
request to database:
对数据库的请求:
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from UserPattern where user = :user_id and pattern = :pattern_id ");
query.setParameter("user_id", userId);
query.setParameter("pattern_id", pattern_id);
List<UserPattern> list = query.list();//exception throws here
I got following exception:
我得到以下异常:
...
java.lang.IllegalArgumentException: Can not set java.lang.Integer field
com.....s.model.User.id to java.lang.Integer
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.lang.reflect.Field.get(Field.java:379)
....
Please help to fix this issue.
请帮助解决这个问题。
error message looks very very strange.
错误信息看起来非常非常奇怪。
I have read related topic clickbut I don't found out answer.
我已阅读相关主题点击,但我没有找到答案。
P.S.
聚苯乙烯
hibernate log(before exception):
休眠日志(异常前):
Hibernate:
select
userpatter0_.id as id1_2_,
userpatter0_.amountSearched as amountSe2_2_,
userpatter0_.amountplayed as amountpl3_2_,
userpatter0_.pattern_id as pattern_4_2_,
userpatter0_.user_id as user_id5_2_
from
UserPattern userpatter0_
where
userpatter0_.user_id=?
and userpatter0_.pattern_id=?
In browser I see following message:
在浏览器中,我看到以下消息:
HTTP Status 500....could not get a field value by reflection getter of...model.User.id
采纳答案by Pimgd
What happens if you change your HQL query to from UserPattern where user.id = :user_id and pattern.id = :pattern_id
?
如果您将 HQL 查询更改为 ,会发生什么from UserPattern where user.id = :user_id and pattern.id = :pattern_id
?
I think Hibernate is confusing objects and ID fields.
我认为 Hibernate 混淆了对象和 ID 字段。
回答by Priyesh
You need to modify your query as follows:
您需要按如下方式修改您的查询:
from UserPattern where user.id = :user_id and pattern.id = :pattern_id
In your query, you are trying to match a User
object with an Integer
object.
在您的查询中,您试图将一个User
对象与一个Integer
对象进行匹配。
回答by dieter
If your field name is "id", your getter and setter methods should be named
如果你的字段名称是“id”,你的 getter 和 setter 方法应该被命名
public Integer getId(){return id;}
public void setId(Integer id){this.id = id};
If your are using Eclipse, generate the getter/setter by right click -> Source -> Generate Getters and Setters...
如果您使用的是 Eclipse,请通过右键单击 -> 源 -> 生成 Getter 和 Setter 来生成 getter/setter...
Make sure your getters and setter are public. Also you should add @Table-Annotation to all your Entities
确保你的 getter 和 setter 是公开的。您还应该将@Table-Annotation添加到所有实体
回答by T4Technologic
i think maybe your annotation should be
我想也许你的注释应该是
@ManyToOne(TargetEntity=....class)
@ManyToOne(TargetEntity=....class)