[英]Spring Pageable doesn't work for ordering
在互聯網上,我發現Spring可以進行分頁以及訂購從數據庫中檢索到的數據列表。 因此,我創建了如下的測試類:
@Test
public void testPageable() {
int pageSize = 5;
Sort sort = new Sort( Direction.DESC, "someColumnA" );
Pageable pageable = new PageRequest( 0, pageSize, sort );
List<SomeObject> listOFSomeObject = getDao().getListData( "paramOne", pageable );
}
當我分析列表時,盡管我只得到5條正確的記錄,但我從未以DESC方式獲得someColumnA的順序。
有人可以讓我知道我可能做錯了嗎? 就像FYI一樣,我正在使用Hibernate進行數據庫訪問和使用Spring命名查詢。
編輯:getListData()->的代碼
public interface SomeRepository
extends JpaRepository<EntityMappedViaHibernate, String> {
List<Object[]> getListData( @Param(value = PARAM_ONE) final String paramOne, Pageable pageable );
}
我的Hibernate實體如下:
@NamedQueries(value = {
@NamedQuery(name = "SomeRepository.getListData", query = "select id, someColumnA from EntityMappedViaHibernate where id = :paramOne")
})
@Entity
@Table(name = "entity_mapped_via_hibernate")
public class EntityMappedViaHibernate implements Serializable {
// Code Omitted on purpose
}
因此,像我一樣在奮斗中的人們,這個問題的解決方案是,您需要在存儲庫中添加查詢。 就我而言,它必須位於SomeRepository中。 因此,在倉庫代碼中執行以下操作:
public interface SomeRepository
extends JpaRepository<EntityMappedViaHibernate, String> {
@Query("select id, someColumnA from EntityMappedViaHibernate where id = :paramOne")
List<Object[]> getListData( @Param(value = PARAM_ONE) final String paramOne, Pageable pageable );
}
無需在EntityMappedViaHibernate內部包含NamedQuery。 而已!!
希望有人找到答案,不要像我一樣掙扎。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.