简体   繁体   English

无法将 java.lang.Integer 字段设置为 java.lang.ZA0FAEF0851B4194C46F2B2B

[英]Can not set java.lang.Integer field to java.lang.Integer

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 click but I don't found out answer.我已阅读相关主题点击,但我没有找到答案。

PS附言

hibernate log(before exception): hibernate 日志(异常前):

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

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字段。

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对象进行匹配。

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, 通过右键单击 - > Source - > Generate Getters and Setters生成getter / setter ...

Make sure your getters and setter are public. 确保你的getter和setter是公开的。 Also you should add @Table -Annotation to all your Entities 您还应该为所有实体添加@Table -Annotation

i think maybe your annotation should be 我想也许你的注释应该是

@ManyToOne(TargetEntity=....class) @ManyToOne(TargetEntity = ....类)

I struggled with this for days and have finally found the answer.我为此苦苦挣扎了几天,终于找到了答案。
You used to be able to tell Hibernate what type the parameter was by using alternatives to setParameter() such as setInteger etc. But these are now deprecated.您曾经可以通过使用 setParameter() 的替代方法(例如 setInteger 等)来告诉 Hibernate 参数是什么类型。但这些现在已被弃用。

The problem is Hibernate is failing to interpret the type correctly for some reason.问题是 Hibernate 由于某种原因未能正确解释类型。 But there is an overloaded version of setParameter which allows you to set the type directly.但是有一个重载版本的 setParameter 允许您直接设置类型。

Firstly import the types: import org.hibernate.type.StandardBasicTypes;首先导入类型: import org.hibernate.type.StandardBasicTypes;

Then use the following syntax to set the Parameter: q.setParameter("name", variable containing the value, StandardBasicTypes.INTEGER);然后使用以下语法设置参数: q.setParameter("name", variable contains the value, StandardBasicTypes.INTEGER);

So in your case this would look like q.setParameter("user_id", userid, StandardBasicTypes.INTEGER);因此,在您的情况下,这看起来像 q.setParameter("user_id", userid, StandardBasicTypes.INTEGER);

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM