错误: - org.hibernate.PropertyAccessException:无法通过反射getter获取字段值

时间:2016-06-15 17:49:56

标签: java hibernate hibernate-mapping hibernate-criteria hibernate-annotations

I have Class Customer ,User , Customer has property manager of user class 

Class Customer {

/** The manager. */
    @ManyToOne(optional = false, cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
    @JoinColumn(name = "MANAGER")
    @JsonSerialize(using = EntitySerializer.class)
    @JsonDeserialize(using = UserDeserializer.class)
    private User manager;
}
-------------------------------------
Class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = User.TABLE_NAME + "_SEQUENCE")
    @SequenceGenerator(name = User.TABLE_NAME + "_SEQUENCE", sequenceName = User.TABLE_NAME + "_SEQ")
    @Column(name = FIELD_ID, nullable = false)
    @SuppressWarnings("PMD.ShortVariable")
    private Integer id;

@Override
public Integer getId() {
        return id;
    }

@Override
    public void setId(final Integer newId) {
        //System.out.println("setID");
        id = newId;
    }
}

现在我正在尝试创建标准

final Criteria criteria = getSession()。createCriteria(Customer.class); criteria.add(Restrictions.ilike("经理","%" + searchTerm +"%"))

抛出错误: - org.hibernate.PropertyAccessException:无法通过com.User.id的反射getter获取字段值

引起: java.lang.IllegalArgumentException:无法将java.lang.Integer字段com.User.id设置为java.lang.String

** Id字段为整数**

3 个答案:

答案 0 :(得分:0)

请您更改以下内容:

final Criteria criteria = getSession().createCriteria(Customer.class); criteria.add(Restrictions.ilike("manager", "%"+searchTerm+"%"))

通过以下内容:

final Criteria criteria = getSession().createCriteria(Customer.class); criteria.add(Restrictions.ilike("manager.name", "%"+searchTerm+"%"))

LIKE子句仅适用于文本列。

答案 1 :(得分:0)

使用此代码

return this.sessionFactory.getCurrentSession()
                          .createCriteria(UserTraining.class) 
                          .add(Restrictions.eq("userProfile.userId", userId))  
                          .list();

答案 2 :(得分:-1)

您使用此注释来删除错误

@Table(name="user_training")
@Entity
public class UserTraining {

    @Id
    @GeneratedValue
    @Column(name="id")
    private int id;

    //Generate getter setter of id
/*
    @Column(name="user_id")
    private int userId;

    */


    @JsonIgnore
    @ManyToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name = "user_id")
    private UserProfile userProfile;


    public UserProfile getUserProfile() {
        return userProfile;
    }


    public void setUserProfile(UserProfile userProfile) {
        this.userProfile = userProfile;
    }

    @OneToOne
    @JoinColumn(name = "training_id")
    private Training training;

    @Column(name="view_count")
    private int viewCount;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    public Training getTraining() {
        return training;
    }

    public void setTraining(Training training) {
        this.training = training;
    }

    public int getViewCount() {
        return viewCount;
    }

    public void setViewCount(int viewCount) {
        this.viewCount = viewCount;
    }

}