Question

I occured a Hibernate Query problem when I's attempting to query data by LIKE operator with parameterized value, the following is my code fragment for doing it:

String hql = "FROM Customer WHERE username LIKE :username";
Query query =sessionFactory.getCurrentSession().createQuery(hql);
query.setParameter("username", "%" + keyword + "%")

The above code runs alwasy with exception:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: LIKE near line 1

It's ok for using other query operators like =, !=, > with parameterized values, and just get confused to this and how can I use Hibernate query's like operator with parameterized values?

Thanks in advance!

Was it helpful?

Solution

String hql = "FROM Customer WHERE username LIKE ':username'";
Query query =sessionFactory.getCurrentSession().createQuery(hql);
query.setParameter("username", "%" + keyword + "%")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top