Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate HQL query by using like operator

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 ?

like image 905
Arthur Ronald Avatar asked Dec 14 '09 18:12

Arthur Ronald


People also ask

How do you use like clause in HQL?

Well, LIKE operator is usually used with textual data i.e. with VARCHAR or CHAR columns, and you have numeric id column (INTEGER). Maybe you could try to map id field also as string and use that field in query. This may or may not work depending on your database engine.

Is HQL similar to SQL?

Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.

Is JPQL and HQL same?

The Hibernate Query Language (HQL) and Java Persistence Query Language (JPQL) are both object model focused query languages similar in nature to SQL. JPQL is a heavily-inspired-by subset of HQL. A JPQL query is always a valid HQL query, the reverse is not true however.

Does hibernate support union HQL query?

I know hibernate does not support union queries at the moment, right now the only way I see to make a union is to use a view table.


2 Answers

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

like image 97
Arthur Ronald Avatar answered Oct 23 '22 10:10

Arthur Ronald


Well, LIKE operator is usually used with textual data i.e. with VARCHAR or CHAR columns, and you have numeric id column (INTEGER).

Maybe you could try to map id field also as string and use that field in query. This may or may not work depending on your database engine. Note that you should handle all updates via setId() and consider idAsString field to be read-only.

@Entity
public class User {

    private Integer id;
    private String idAsString;

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

    private void setId(Integer id) {
        this.id = id;
    }

    @Column(name="id", insertable=false, updatable=false)
    private String getIdAsString() {
       return this.idAsString;
    }

    private void setIdAsString(String idAsString) {
       this.idAsString = idAsString;
    }
}

Then the query would be:

Query query = sessionFactory.getCurrentSession().createQuery("from User u where u.idAsString like :userId");
List<User> userList = query.setParameter("userId", userId + "%").list();
like image 38
Juha Syrjälä Avatar answered Oct 23 '22 11:10

Juha Syrjälä