Fabricio Z
Fabricio Z

Reputation: 23

Spring JPA @Query where clause with 'like' and 'or'

I have the following query:

@Query("select c from Category c where ( (lower(c.name) like '%' || lower(:searchText) || '%') or (lower(c.description) like '%' || lower(:searchText)) || '%')")

My product is designed to run in multiple platform, I am getting an error on postgreSQL which is:

PSQLException: ERROR: argument of OR must be type boolean, not type text.

Which is undestandable since the like clause return strings. But I wasn't able to perform the search in one query request. So the question is how can I perform a search where the where conditions refer to 2 differnt columns and use the 'like' operator.

Upvotes: 2

Views: 4253

Answers (1)

Eelke
Eelke

Reputation: 21993

The parentheses you have are not correct the following should work:

@Query("select c from Category c " +
       "where (lower(c.name) like ('%' || lower(:searchText) || '%')) " +
         " or (lower(c.description) like ('%' || lower(:searchText) || '%'))")

Upvotes: 3

Related Questions