当我检查下面的代码时,我得到了所有记录的计数。不是明智的条件。
CriteriaBuilder builder = getCurrentSession().getCriteriaBuilder();
CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
countQuery.select(builder.count(countQuery.from(ViewEmployees.class)));
Long q1 = getCurrentSession().createQuery(countQuery).getSingleResult();
我想获取具有条件条件的计数。 像这样的代码
CriteriaQuery<ViewEmployees> query = builder.createQuery(ViewEmployees.class);
Root<ViewEmployees> root = query.from(ViewEmployees.class);
List<Predicate> predicates = new ArrayList<Predicate>();
// Equal Query
if (StringUtils.isNotBlank(searchConditions.getCode())) {
predicates.add(builder.and(builder.equal(root.get("code"), searchConditions.getCode())));
}
它使用下面的代码运行较旧的版本。
Criteria criteria = getApplicationEventConditions(searchCond);
SearchTotals searchTotals = new SearchTotals();
ProjectionList p1 = Projections.projectionList();
p1.add(Projections.rowCount());
criteria.setProjection(p1);
List l = criteria.list();
searchTotals.setTotalSize((Long) l.get(0));
可以请任何人帮助我使用CriteriaBuilder解决此问题。