hibernate - 如何在标准 api 中选择 count(*)?
问题描述
我有一个由休眠条件 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 查询?
解决方案
使用count(1)
相当于做criteriaBuilder.count(criteriaBuilder.literal(1))
推荐阅读
- javascript - 使用 js 将服务器响应转换为有效对象
- python - pyglet.gl AttributeError:“int”对象没有属性“_get_domain”
- spring-boot - 为什么执行测试类时特定测试失败 - JUnit 5,但单独运行时没有
- javascript - THREE.WebGLShader 创建大量点光源时出现“错误:对许多制服”
- c# - 如何使用 .NET 6 preview 7 中的 IAdditionOperators<,,>?
- c++ - 可变参数宏可以使用 clang 正确评估,但不能使用 windows 编译器
- javascript - 使用 Firestore setDoc 方法时出现“意外的保留字 'await'”错误
- mysql - 运行 Sql 迁移会引发语法错误
- python - 如何使用 for 循环将线性回归结果(变量名、p_value)放入数据框中?
- css - 将 XPATH 转换为 CSS 选择器工具