使用CriteriaBuilder执行查询和记录计数

时间:2014-01-17 11:47:48

标签: sql primefaces criteria-api

我使用CriteriaBuilder构建的自定义复杂动态查询懒洋洋地填充Primefaces数据表,在数据库上执行SQL查询。

为此我需要使用此查询执行记录计数,我还需要运行查询本身以获取数据表的指定时间间隔内的记录。

所以我认为我可以这样做:

    CriteriaBuilder criteriaBuilder = this.getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Object> criteriaQuery = criteriaBuilder.createQuery();
    Root<RSip> from = criteriaQuery.from(RSip.class);
    Path<Long> eSipss = from.join("idESip").get("ss");
    CriteriaQuery<Object> select = criteriaQuery.select(from);
    List<Predicate> predicates = new ArrayList();
    //Many predicates added according to user end
    predicates.add(...);
    predicates.add(...);
    predicates.add(...);
    //The query is now ready
    select.where(predicates.toArray(new Predicate[predicates.size()]));

    //but I need also to perform the record count using SQL Count as the dataset returned can be very large
    CriteriaQuery<Object> selectCount = criteriaQuery.select(criteriaBuilder.count(fromCount));


   //and then perform both selects like this:
   //Record Count:
    TypedQuery<Object> typedQueryCount = this.getEntityManager().createQuery(selectCount);
    List<Object> recordCount = typedQueryCount.getResultList();
   //Query:
    TypedQuery<Object> typedQuery = this.getEntityManager().createQuery(select);
    List<Object> records = typedQuery.getResultList();

问题是,在第二个查询中,它还返回记录计数而不是实际记录......我可能做错了什么?

如果还有其他方法可以做到这一点,我很高兴看到你的答案!

0 个答案:

没有答案