简体   繁体   中英

Error :- org.hibernate.PropertyAccessException: could not get a field value by reflection getter

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;
    }
}

Now when i am trying to create criteria

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

It throwing Error :- org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.User.id

Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Integer field com.User.id to java.lang.String

**Id field is integer **

Could you please change the following:

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

by the following:

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

LIKE clause is applicable to text column only.

this code to used

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

You used this annotation to error remove

@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;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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