0

I want to implement JPA Repository with sort direction for private LocalDateTime created_at;. I tried this:

Service

@Override
public Page<PaymentTransactions> findAll(int page, int size) {
    return dao.findAllByCreated_atDesc(PageRequest.of(page, size));
}

Repository

@Repository
public interface PaymentTransactionRepository extends JpaRepository<PaymentTransactions, Integer> {

    Page<PaymentTransactions> findAllByCreated_atDesc(PageRequest of);
}

But I get error:

 No property created found for type PaymentTransactions!

Do you know how I can implement this example properly?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

0

You can do this:

Service

@Override
public Page<PaymentTransactions> findAll(int page, int size) {
    return dao.findAll(PageRequest.of(page,size, new Sort(Sort.Direction.DESC, "created_at")));
}
earandap
  • 1,446
  • 1
  • 13
  • 22
  • Thanks, What should I declare into the repository? – Peter Penzov Apr 15 '19 at 21:07
  • Nothing. JpaRepository extend of PagingAndSortingRepository . This is the method that the above example is using Page findAll(Pageable pageable); https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/PagingAndSortingRepository.html – earandap Apr 15 '19 at 21:08
  • Is there a way to implement a method into the repository because I want to track everything into one repository? – Peter Penzov Apr 15 '19 at 21:10
  • Sure. Page findAllOrderByCreated_atDesc(PageRequest of); – earandap Apr 15 '19 at 21:15
  • But you need to escape the _ character. Take a look here: https://stackoverflow.com/questions/23456197/spring-data-jpa-repository-underscore-on-entity-column-name – earandap Apr 15 '19 at 21:19
  • I gee `No property created found for type PaymentTransactions!` Probably the underscore `_` is the problem? – Peter Penzov Apr 15 '19 at 21:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191876/discussion-between-earandap-and-peter-penzov). – earandap Apr 15 '19 at 21:19
  • Do you tried to use findAllByCreatedAtDesc instead of findAllByCreated_atDesc. – Sma Ma Apr 15 '19 at 22:25
  • @SmaMa I tried but I get No property desc found for type LocalDateTime! – Peter Penzov Apr 16 '19 at 08:45