javasqlspringhibernatehsqldb

How to use LIKE clause with % in Hibernate?


How do I use LIKE with % in Hibernate. I want to use SQL's LIKE clause with % in my hbm.xml file. I have 2 queries that I am consolidating to 1.

The query looks like this:

select * from PAY_GROUP_VW where CASE_SID=? AND CASE_TLE like %?%

I tried %?%, but using % does not work. I have used LIKE clause without % and it works.

This is how I pass the parameters:

q.setParameter(0, csId);
q.setParameter(1, csTle);
return q.list();

Tried like %||? and got the following exception:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:630) at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424) at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:343)


Solution

  • Maybe the user_id is not a char/varchar? You have to convert the user_id with str() to character data first!

    Example:

    SELECT *
    FROM abc.def_vw a
    WHERE user_id = ?
    AND str(user_id) LIKE '%' || ?