Spring DATA JPA findAll carries out OrderBy mode

  • 2021-12-13 07:52:55
  • OfStack

Directory Spring DATA JPA findAll OrderBySpring Data JPA uses one pit of orderby

OrderBy for findAll in Spring DATA JPA

You need to define such a method in repository:


findAllByOrderByUpdatedAtDesc () 

public List findAllByOrderByUpdatedAtDesc();

Important: (Add an extra By in the middle)


findAllByOrderByUpdatedAtDesc();

The little 'By' does all the magic.

Spring Data JPA uses one pit of orderby

According to the JPA writing that can be found on the Internet, I want to find all the data and arrange it in ascending order according to rank. The code is as follows


public interface RsEventRepository extends CrudRepository<RsEventDto, Integer> {
  List<RsEventDto> findAllByOrderByRankAsc();
  }

Results java reported an error, and then looked for problems in entity and repository.

java.lang.IllegalStateException: Failed to load ApplicationContext

No property asc found for type int! Traversed path: RsEventEntity.rank.

Finally, it is found that by should be added after ALL


List<RsEventDto> findAllByOrderByRankAsc();

In this way, I can query according to my idea.

Sure enough, JPA writes CRUD by guessing -_-


Related articles: