Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA: case insensitive orderBy

I want to sort my entities by the name attribute with ascending direction and ignoring the case.

I've created an EntityRepository extending the Repository interface of Spring Data. Then I've declared the following find method:

List<Entity> findByNameOrderByNameIgnoreCaseAsc(String name);

But I get this error "No property ignoreCase found for type".

I can't find any reference to this case in the Spring Data JPA documentation.

I'm using spring-data-jpa version 1.11.0.

like image 785
Fedy2 Avatar asked Mar 10 '23 12:03

Fedy2


2 Answers

If you want just to return all entities with sorting by name with ignore case you can use this:

repository.findAll(Sort.by(Sort.Order.asc("name").ignoreCase()));
like image 122
Aliaksandr Hamayunau Avatar answered Mar 20 '23 10:03

Aliaksandr Hamayunau


Spring Data IgnoreCase can be only used for finding by a property, and cannot be user in combination with Order by.

However you can get you results using Sort.Order.ignoreCase() and Pages http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Sort.Order.html#ignoreCase--

like image 28
Aboullaite Avatar answered Mar 20 '23 11:03

Aboullaite