Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jpa criteriabuilder upper gives compilation error

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

like image 494
matteo.cajani Avatar asked Jul 17 '14 16:07

matteo.cajani


1 Answers

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.

like image 197
Koitoer Avatar answered Nov 03 '22 14:11

Koitoer