标准生成器忽略大小写查询

如何使用条件生成器执行类似的忽略大小写查询。对于描述属性,我想执行类似于 upper(description) like '%xyz%'的操作

我有以下问题

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();
56072 次浏览

There is a CriteriaBuilder.upper() method:

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

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()+"%"));