gstackoverflow gstackoverflow - 1 month ago 4x
Java Question

Can not set java.lang.Integer field to java.lang.Integer

User declaration:

public class User {
private Integer id;

Pattern declaration:

public class Pattern {
Integer id;


public class UserPattern {
Integer id;

@JoinColumn(name = "user_id")
User user;

@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 to java.lang.Integer
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(
at java.lang.reflect.Field.get(

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.


hibernate log(before exception):

select 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_
UserPattern userpatter0_
and userpatter0_.pattern_id=?

In browser I see following message:

HTTP Status 500....could not get a field value by reflection getter


What happens if you change your HQL query to from UserPattern where = :user_id and = :pattern_id?

I think Hibernate is confusing objects and ID fields.