简体   繁体   English

Spring Pageable不适用于订购

[英]Spring Pageable doesn't work for ordering

On the internet I found that Spring can do pagination as well as ordering for a list of data retrieved from the database. 在互联网上,我发现Spring可以进行分页以及订购从数据库中检索到的数据列表。 Accordingly, I created my test class as following: 因此,我创建了如下的测试类:

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

When I analyze the List I never get ordering of someColumnA in a DESC fashion, although I get back only 5 records which is correct. 当我分析列表时,尽管我只得到5条正确的记录,但我从未以DESC方式获得someColumnA的顺序。

Can someone please let me know what I might be doing wrong? 有人可以让我知道我可能做错了吗? Just as an FYI, I am using Hibernate for database access and Spring named query. 就像FYI一样,我正在使用Hibernate进行数据库访问和使用Spring命名查询。

EDIT: Code for getListData()-> 编辑:getListData()->的代码

public interface SomeRepository
                extends JpaRepository<EntityMappedViaHibernate, String> {

    List<Object[]> getListData( @Param(value = PARAM_ONE) final String paramOne, Pageable pageable );
}

My Hibernate entity is as follows: 我的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
}

So those of you who are struggling like me the solution to this problem is that you need to have the query inside the Repository. 因此,像我一样在奋斗中的人们,这个问题的解决方案是,您需要在存储库中添加查询。 In my case it has to be inside the SomeRepository. 就我而言,它必须位于SomeRepository中。 So in the code of the repo do the following: 因此,在仓库代码中执行以下操作:

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

No need to have the NamedQuery inside the EntityMappedViaHibernate. 无需在EntityMappedViaHibernate内部包含NamedQuery。 That's it!! 而已!!

Hope someone find the answer and do not struggle like I did. 希望有人找到答案,不要像我一样挣扎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM