【问题标题】:How to select count(*) in criteria api?如何在标准 api 中选择 count(*)?
【发布时间】:2020-12-10 10:41:04
【问题描述】:

我有一个由休眠条件 api 生成的查询,它需要很长时间才能执行:

select count (entity.id) 
from table 
where field1 in ('...') and field2 in ('...') and ...

我已将 entity.id 替换为 '*':

select count (*) 
from table 
where field1 in ('...') and field2 in ('...') and ...

由于某些原因,现在它运行良好,但我无法通过条件 api 生成此查询。我正在像这样创建 Root:

Root<MyEntity> root cq.from(MyEntity.class);

有什么方法可以用 select count(*) 而不是 count(id) 生成 sql 查询?

【问题讨论】:

标签: hibernate jpa hibernate-criteria criteria-api


【解决方案1】:

使用count(1),相当于criteriaBuilder.count(criteriaBuilder.literal(1))

【讨论】:

    猜你喜欢
    • 2011-03-29
    • 2019-11-18
    • 1970-01-01
    • 2013-06-19
    • 2016-01-25
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2011-01-24
    相关资源
    最近更新 更多