criteria-api

CriteriaAPI like predicate behaving unexpectedly after switching to v3.1


We recently switched to jakarta.persistence-api-3.1.0 and since then, a test is failing. I traced it back to this line:

criteriaBuilder.like(file.get("name"), "%" + filename + "%");

file is a Path to a File entity with the field name. filename is the name we want to filter for - here it's "TEST". However, this predicate results in an empty list when applied to a query. Same for criteriaBuilder.like(file.get("name"), "%") which would have resulted in a default true / no filtering with our old version.

What DOES work is

criteriaBuilder.equal(file.get("name"), filename);

so I'm positive that something changed with the criteriaBuilder.like method, but I haven't found anything in the docs. What am I missing?


Solution

  • The solution was to add an escape character:

    criteriaBuilder.like(file.get("name"), "%" + filename + "%", '\\');
    

    Otherwise, wildcards such as % or _ won't get treated as wildcards.