10
votes

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

1

1 Answers

22
votes

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.