您的代码失败,因为您
Root为count和where子句使用了不同的实例:第一个(按定义顺序)生成
generatedAlias1别名,另一个生成
generatedAlias0。您需要重构代码,以便
Root在两个地方使用相同的实例:
CriteriaQuery<Long> cqCount = builder.createQuery(Long.class);Root<ReqStatCumulative> entityRoot = cqCount.from(cqEntity.getResultType());cqCount.select(builder.count(entityRoot));Path<Long> processTimeSeconds = entityRoot.get("processTimeSeconds");cqCount.where(qb.and(qb.greaterThan(processTimeSeconds, (long) cfg.getProcessTimeExceedsSec()),// qb.lessThan(processTimeSeconds, (long) cfg.getProcessTimeExceedsSec() + 100))// );// return em.createQuery(cqCount).getSingleResult();


