java 如何在 Spring Data JPA 中将分页与条件查询结合起来?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12782023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:11:54  来源:igfitidea点击:

How to combine pagination with a criteria query in Spring Data JPA?

javaspringjpaspring-dataspring-data-jpa

提问by Mariusz

I use PagingAndSortingRepository and findAll(Pageable pageable) method to paging my data. I think that there is no way to provide any condition. For example sometime I want select and paging addresses where city=NY. Is there any way to provide condition and paging simultaneously?

我使用 PagingAndSortingRepository 和 findAll(Pageable pageable) 方法来分页我的数据。我认为没有办法提供任何条件。例如,有时我想要选择和寻呼城市 = NY 的地址。有没有办法同时提供条件和分页?

采纳答案by Oliver Drotbohm

The PagingAndSortingRepositoryjust adds the very basic CRUD methods in pagination mode. As the reference documentationdescribes, you can simply add a Pageableparameter to any query method you define to achieve pagination of this query.

PagingAndSortingRepository只是增加了在分页模式非常基本的CRUD方法。正如参考文档所述,您可以简单地Pageable向您定义的任何查询方法添加一个参数,以实现此查询的分页。

interface CustomerRepository implements Repository<Customer, Long> {

  Page<Customer> findByLastname(String lastname, Pageable pageable);

  List<Customer> findByFirstname(String firstname, Pageable pageable);
}

The first method will return a Pagecontaining the page metadata like total elements available etc. To calculate this data it will trigger an additional count query for the query derived. The second query method returns the plain sliced result set without the count query being executed.

第一种方法将返回一个Page包含页面元数据(如可用元素总数等)的数据。为了计算此数据,它将为导出的查询触发额外的计数查询。第二个查询方法返回未执行计数查询的普通切片结果集。

回答by Rahul Agrawal

For Pagination you need 2 methods like getCount(Pageable pageable) and findAll(Pageable pageable)

对于分页,您需要 2 个方法,例如 getCount(Pageable pageable) 和 findAll(Pageable pageable)

Utilizing this functionality in Hibernate, however, is trivial. If you have any HQL query you can simply do this:

然而,在 Hibernate 中使用这个功能是微不足道的。如果您有任何 HQL 查询,您可以简单地执行以下操作:

public Long getCount(Pageable pageable) {
    Query q = session.createQuery("Select count(t) From TableClass  t where t.city = 'NY'");
    Long cnt = (Long) q.uniqueResult();
    return cnt;
}

public List<TableClass> findAll(Pageable pageable) {
     Query q = session.createQuery("From TableClass  t where t.city = 'NY'");
     q.setFirstResult(start);
     q.setMaxResults(length);
     List<TableClass> tableClasslist = q.list();
     return tableClasslist;
}

Likewise, if you have a Criteria query, it's effectively the same thing:

同样,如果你有一个 Criteria 查询,它实际上是一样的:

public Long getCount(Pageable pageable) {
    Criteria c = session.createCriteria(TableClass.class);
    c.setProjection(Projections.rowCount()); 
    Long cnt = (Long) c.uniqueResult();
    return cnt;
}

public List<TableClass> findAll(Pageable pageable) {
    Criteria c = session.createCriteria(TableClass.class);
    c.setParameter("city","NY");
    c.setFirstResult(start);
    c.setMaxResults(length);
    List<TableClass> tableClasslist = c.list();
    return tableClasslist ;
}