2017-06-09 26 views
0

在互联网上,我发现Spring可以做分页以及从数据库中检索数据列表。因此,我创建了测试类如下:Spring Pageable并不适用于订购

@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); 
} 

当我分析清单我从来没有someColumnA的排序在DESC时尚,虽然我回来只有5条记录这是正确的。

有人可以让我知道我可能会做错什么吗?就像一个参考,我使用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 
} 
+0

你可以添加'getListData'的代码吗? –

+0

@Darshan,添加了所需的代码。请看看 – Raj

+0

不确定它是否正确,但方法需要返回'。你可以尝试改变它吗? –

回答

0

因此,那些你们谁是挣扎和我一样,要解决这个问题您需要在存储库中查询。在我的情况下,它必须在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。而已!!

希望有人找到答案,不要像我那样挣扎。