JPA/Criteria API - Like & equal problem
Solution 1:
Perhaps you need
criteria.add(cb.like(emp.<String>get("name"), p));
because first argument of like()
is Expression<String>
, not Expression<?>
as in equal()
.
Another approach is to enable generation of the static metamodel (see docs of your JPA implementation) and use typesafe Criteria API:
criteria.add(cb.like(emp.get(Employee_.name), p));
(Note that you can't get static metamodel from em.getMetamodel()
, you need to generate it by external tools).
Solution 2:
Better: predicate (not ParameterExpression
), like this :
List<Predicate> predicates = new ArrayList<Predicate>();
if(reference!=null){
Predicate condition = builder.like(root.<String>get("reference"),"%"+reference+"%");
predicates.add(condition);
}
Solution 3:
It will work with a small addition of .as(String.class)
:
criteria.add(cb.like(emp.get("name").as(String.class), p));
Solution 4:
Use :
personCriteriaQuery.where(criteriaBuilder.like(
criteriaBuilder.upper(personRoot.get(Person_.description)),
"%"+filter.getDescription().toUpperCase()+"%"));