Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Criteriabuilder lower() vs Java toLowerCase()

According to this Link

Predicate lcSurnameLikeSearchPattern = criteriaBuilder.like( criteriaBuilder.lower(Person_.surname), searchPattern.toLowerCase());

This seems like Hibernate would generate an SQL that would look something like

LOWER(PERSON.SURNAME) LIKE 'searchPattern'

Where searchPattern would have been lowered in Java using whatever implementation toLowerCase would provide. Whereas the query LOWER would use Oracle implementation. For ASCII characters, I'm guessing things are pretty simple, but would there ever be discrepancies on international UTF characters?

How would I get JPA to lower both operands of LIKE in the query? So the generated query looks like

LOWER(PERSON.SURNAME) LIKE LOWER('searchPattern')
like image 440
BKaun Avatar asked Mar 13 '23 12:03

BKaun


1 Answers

You can use two Expressions as like method parameters and literal to wrap a String into Expression:

Predicate lcSurnameLikeSearchPattern = criteriaBuilder.like(
  criteriaBuilder.lower(Person_.surname),
  criteriaBuilder.lower(criteriaBuilder.literal(searchPattern))
);

CriteriaBuilder#like(Expression,Expression)

CriteriaBuilder#lower(Expression)

CriteriaBuilder#literal(T)

I ommit Person_.surname definition as it is from your example.

like image 124
Vlastimil Ovčáčík Avatar answered Mar 24 '23 17:03

Vlastimil Ovčáčík