10

I'm tying to use CriteriaBuilder for a case insensitive query as described here hibernate jpa criteriabuilder ignore case queries and in many other questions and tutorials around the web.

My code is:

public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
    return builder.equal(builder.upper(root.get("firstName")), "test".toUpperCase());
}

but I'm getting a compile time error:

The method upper(Expression<String>) in the type CriteriaBuilder is not applicable for the arguments (Path<Object>)

The version of hibernate jpa I'm using is:

<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>

Does it depend on the hibernate version I'm using? How to put an Expression<String> there instead of a Path<Object>?

Thank you for your help

Community
  • 1
  • 1
matteo.cajani
  • 2,495
  • 4
  • 21
  • 19

1 Answers1

23

As compiler said we it is expecting Expression in this case Path extends from Expression but you have a Path to fix this issue due the following.

return builder.equal(builder.upper(root.<String> get("firstName")), "test".toUpperCase());

Trick is add <String> before get method, hope that helps.

Koitoer
  • 18,778
  • 7
  • 63
  • 86
  • thank you! that was the problem, I was wondering about Expression vs Path and I was missing the point that it was expecting a String instead of an Object – matteo.cajani Jul 18 '14 at 08:02
  • 1
    This helped me as well. Thank you. Btw here is my little snippet incase this can help someone else. criteriaQuery.select(cb.greatest(from.get(EVENT_TIME_STAMP))); – Dan Whitehouse Feb 16 '18 at 03:48