Spring Data JPA: case insensitive orderBy

2019-07-26 08:31发布

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.

3条回答
再贱就再见
2楼-- · 2019-07-26 08:49

Your method should be like this.

List<Entity> findByNameIgnoreCaseOrderByNameAsc(String name);

Please refer https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation

查看更多
【Aperson】
3楼-- · 2019-07-26 08:57

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()));
查看更多
走好不送
4楼-- · 2019-07-26 09:05

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--

查看更多
登录 后发表回答