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,這樣才可以查詢到想要的數據,真的令人吐血。