我需要获取所有文件,按照某个fied(parent
在我的情况下)排序。
在我的ItemRepository
我添加了以下签名:
public List<Item> findAllOrderByParent();
但是通过调用它,我得到了一个
org.springframework.web.util.NestedServletException: Request processing failed;
nested exception is
org.springframework.data.repository.query.ParameterOutOfBoundsException:
Invalid parameter index! You seem to have declare too little query method parameters!
同样将Asc
或Desc
推迟到方法名称。
我在某处阅读了正确的语法应该是以下内容(请注意By
之后的额外findAll
):
public List<Item> findAllByOrderByParent();
我来了
NullPointerException
Caused by:
java.lang.NullPointerException
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.count(ElasticsearchTemplate.java:333)
at org.springframework.data.elasticsearch.repository.query.ElasticsearchPartQuery.execute(ElasticsearchPartQuery.java:54)
...
我排除了其他问题,因为声明像公共List<Item> findByNameOrderByParent();
这样的方法可以正常工作。
你有想法继续吗?
谢谢
答案 0 :(得分:2)
这可能有点晚了,但由于问题仍然存在,为了解决这个问题,我只使用了一个使用Sort对象作为参数的findAll方法:
List<Item> findAll(Sort sort);
你称之为:
List<Item> items = this.itemRepository.findAll(new Sort(new Sort.Order(Sort.Direction.DESC, "name")));
&#34;名称&#34;属性,您可以将其提取为常量或使您的服务的listItems方法支持动态排序并将排序字段作为参数发送。
您还可以查看这篇文章:http://maciejwalkowiak.pl/blog/2012/05/09/sorting-spring-data-mongodb-collections-using-orderby/