使用JPA Criteria API进行分页的总行数

时间:2011-03-18 07:39:09

标签: java hibernate search jpa pagination

我正在为我的系统中的实体实现“高级搜索”类功能,以便用户可以使用此实体的属性上的多个条件(eq,ne,gt,lt等)搜索该实体。我正在使用JPA的Criteria API动态生成Criteria查询,然后使用setFirstResult()& setMaxResults()支持分页。到目前为止一切都很好,但现在我想在结果网格上显示结果总数,但我没有看到直接获得Criteria查询总数的方法。
这就是我的代码的样子:

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Brand> cQuery = builder.createQuery(Brand.class);
Root<Brand> from = cQuery.from(Brand.class);
CriteriaQuery<Brand> select = cQuery.select(from);
.
.
//Created many predicates and added to **Predicate[] pArray**
.
.
select.where(pArray);
// Added orderBy clause
TypedQuery typedQuery = em.createQuery(select);
typedQuery.setFirstResult(startIndex);
typedQuery.setMaxResults(pageSize);
List resultList = typedQuery.getResultList();

我的结果集可能很大,所以我不想加载我的实体进行计数查询,所以告诉我有效的方法来获得像Criteria这样的rowCount()方法的总计数(我认为它在Hibernate的标准中)

4 个答案:

答案 0 :(得分:28)

谢谢弗拉基米尔! 我接受了你的想法,并使用单独的计数查询来使用我现有的谓词数组。最终实现如下:

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Brand> cQuery = builder.createQuery(Brand.class);
Root<Brand> from = cQuery.from(Brand.class);
CriteriaQuery<Brand> select = cQuery.select(from);
.
.
//Created many predicates and added to **Predicate[] pArray**
.
.
CriteriaQuery<Long> cq = builder.createQuery(Long.class);
cq.select(builder.count(cq.from(Brand.class)));
// Following line if commented causes [org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'generatedAlias1.enabled' [select count(generatedAlias0) from xxx.yyy.zzz.Brand as generatedAlias0 where ( generatedAlias1.enabled=:param0 ) and ( lower(generatedAlias1.description) like :param1 )]]
em.createQuery(cq);
cq.where(pArray);
Long count = em.createQuery(cq).getSingleResult();
.
.
select.where(pArray);
.
.
// Added orderBy clause
TypedQuery typedQuery = em.createQuery(select);
typedQuery.setFirstResult(startIndex);
typedQuery.setMaxResults(pageSize);
List resultList = typedQuery.getResultList()

虽然这样可以正常工作,但我仍然不确定为什么要写

em.createQuery(cq);

让它运作起来。有什么想法吗?

答案 1 :(得分:14)

为什么不使用计数?

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Long> cQuery = builder.createQuery(Long.class);
Root<Brand> from = cQuery.from(Brand.class);
CriteriaQuery<Long> select = cQuery.select(builder.count(from));
.
.
//Created many predicates and added to **Predicate[] pArray**
.
.
select.where(pArray);
// Added orderBy clause
TypedQuery<Long> typedQuery = em.createQuery(select);
typedQuery.setFirstResult(startIndex);
//typedQuery.setMaxResults(pageSize);
// here is the size of your query 
Long result = typedQuery.getSingleResult();

答案 2 :(得分:2)

如果您正在使用Hibernate,因为您的JPA-Provider会查看projections,尤其是Projections.rowCount()

您可能必须执行两次查询,首先获取计数然后获取结果。

请注意,对于普通的JPA,您可能需要一些其他方法。

答案 3 :(得分:0)

我猜这两个答案都有效。但是它们都不是最优的。 ThinkFloyd的问题在于createQuery被使用了两次。 Vladimir Ivanov创建了两个CriteriaQuery实例,我认为这是不必要的。

val cb = entityManager.criteriaBuilder
val cq = cb.createQuery(ManualQuery::class.java)
val manualQuery = cq.from(ManualQuery::class.java)

val predicates = ArrayList<Predicate>()

/*
predications..... 
*/ 

cq.select(manualQuery)
        .where(*predicates.toTypedArray())
        .orderBy(cb.desc(manualQuery.get<ZonedDateTime>("createdDate")))

val query = entityManager.createQuery(cq)

// total rows count
val count = query.resultList.size

val indexedQuery = query
        .setFirstResult((currentPage - 1) * pageSize)
        .setMaxResults(itemsPerPage)

执行即可。这是在科特林中完成的。您可以在 Java 中以相同的方式进行操作。

相关问题