JPA findAll(spec,Sort)

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

I have this code to get all transaction between 2 dates. I would like to get a desc sorted list. What are the possibilities?

@Override public List<Transaction> searchBySubmitDate(final Date startDate,                                              final Date endDate) {     return transactionRepository.findAll(new Specification<Transaction>() {         @Override         public Predicate toPredicate(Root<Transaction> transaction,                                       CriteriaQuery<?> q, CriteriaBuilder cb) {             Predicate between = cb.between(transaction.get(Transaction_.dateSubmit), startDate, endDate);              return between;         }     }); 

回答1:

@Override     public List<Transaction> searchBySubmitDate(final Date startDate,                                                  final Date endDate) {         return transactionRepository.findAll(new Specification<Transaction>() {             @Override             public Predicate toPredicate(Root<Transaction> transaction,                                           CriteriaQuery<?> q, CriteriaBuilder cb) {                 Predicate between = cb.between(transaction.get(Transaction_.dateSubmit), startDate, endDate);                  return between;             }         },new Sort(Direction.DESC,"dateSubmit")); 


回答2:

The repository has another method taking a Sort as additional argument. Call this method with an appropriate Sort instance.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!