Search code examples
sqlpostgresqljpajpql

jpa postgres - query result difference with like on TEXT fields


I'm trying to make a query to search items structured as follows:

IssueCategory --* Issue (one to many)

using the following JPQL

select count(z) from IssueCategory z join z.issues x
where
lower(cast(function('lo_get', cast(x.diagnosis as integer)) as text)) like lower(concat('TEXT TO SEARCH', '%'))

where diagnosis is a Issue's String field with @Lob annotation, mapped as a text field in postgres:

CREATE TABLE issues (
...
diagnosis text,
...
)

this query produces the following query

select count(issuecateg0_.id) as col_0_0_
from issue_categories issuecateg0_
inner join issues issues1_ on issuecateg0_.id=issues1_.category_id
where lower(cast(lo_get(cast(issues1_.diagnosis as int4)) as text)) like lower(('TEXT TO SEARCH'||'%'))

Obviously in origin the "TEXT TO SEARCH" was passed as a parameter to the query.

The problem is: when I execute the JPQL query, it returns 0, but if I execute the generated query directly in postgres, I get 1.

Does anyone know of behaviours like this one?


Solution

  • I finally changed to the following conditions:

    lower(function('encode', (function('lo_get', cast(x.diagnosis as integer))), 'escape') like lower(concat('TEXT TO SEARCH', '%'))