hibernate jpa criteriabuilder ignorar las consultas de casos

How to do a like ignore case query using criteria builder. For description property I want to do something like upper(description) like '%xyz%'

Tengo la siguiente consulta

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

    CriteriaQuery<Person> personCriteriaQuery = criteriaBuilder.createQuery(Person.class);
    Root<Person> personRoot = personCriteriaQuery.from(Person.class);

    personCriteriaQuery.select(personRoot);
    personCriteriaQuery.where(criteriaBuilder.like(personRoot.get(Person_.description), "%"+filter.getDescription().toUpperCase()+"%"));
    List<Person> pageResults = entityManager.createQuery(personCriteriaQuery).getResultList();

preguntado el 08 de enero de 11 a las 19:01

Sorry, an off-topic question, when do you use the class Person_?, I'm so curious why use underscores in a class name. -

@MarcosEchagüe This classes are generated automatically with hibernate-jpamodelgen. They are used to get the field names instead of using raw Strings. Also refactoring becomes easier with it. For more information have a look at hibernate.org/orm/tooling -

Thanks @felix-seifert!! now i know it! -

2 Respuestas

Hay un CriteriaBuilder.upper() método:

personCriteriaQuery.where(criteriaBuilder.like(
    criteriaBuilder.upper(personRoot.get(Person_.description)), 
    "%"+filter.getDescription().toUpperCase()+"%"));

Respondido el 28 de enero de 21 a las 21:01

If the database contains German words with letters like Fußballschuhe in the column, java will modify the parameter in uppercase method to FUSSBALLSCHUHE and the query will not match. Lowercase will work in this case:

personCriteriaQuery.where(criteriaBuilder.like(
    criteriaBuilder.lower(personRoot.get(Person_.description)), 
    "%"+filter.getDescription().toLowerCase()+"%"));   

Respondido el 19 de Septiembre de 17 a las 20:09

Maybe pass a Locale into the toUpperCase or toLowerCase method? - Brian Blain

The database may also perform the uppercase without considering locale, so make sure that your program is consistent with the database first. - Gagarwa

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.