Spring Data JPA使用findAllOrderBy时踩的坑
按照以往的编程经验,我的写法是这样的:
List<ActivityEntity> findAllOrderByWishCountDesc();
可以看到,我希望在一个表中查询所有的数据,并按照WishCount这个字段进行排序,这样的写法看似正确的,但总会报这样的错误:
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.obowin.sports.business.activity.ActivityRepository.findAllOrderByWishCountDesc()! No property desc found for type Integer! Traversed path: ActivityEntity.wishCount.
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property desc found for type Integer! Traversed path: ActivityEntity.wishCount.
其实,正确的写法是:
List<ActivityEntity> findAllByOrderByWishCountDesc();
需要在findAll后面再加上一个By,这样才可以查询到想要的数据,真的令人吐血。