Hibernate HQL query by using like operator

Arthur Ronald picture Arthur Ronald · Dec 14, 2009 · Viewed 81.1k times · Source

Seu the following mapping

@Entity
public class User {

    private Integer id;

    @Id;
    private Integer getId() {
        return this.id;
    }

}

Notice id is an Integer. Now i need this HQL query by using like operator

Query query = sessionFactory.getCurrentSession().createQuery("from User u where u.id like :userId");

ATT: IT IS like operator NOT = (equals operator)

Then i use

List<User> userList = query.setParameter("userId", userId + "%").list();

But does not work because Hibernate complains IllegalArgumentException occured calling getter of User.id

Even when i use

query.setString("userId", userId + "%");

It does not work

What should i use to pass the query ?

Answer

Arthur Ronald picture Arthur Ronald · Dec 14, 2009

According to Hibernate reference:

str() is used for converting numeric or temporal values to a readable string

So when i use

from User u where str(u.id) like :userId

It works fine