默认情况下,当使用 hibernate 进行计数时,它会生成 count(*) ,是否可以将其更改为默认值 count(1) ?我不想使用 HQL 来执行此操作。我的意思是在使用时 criteria.setProjection(Projections.rowCount());
问问题
872 次
2 回答
1
如果您的查询有 GROUP BY,您可以在 theProjections 类上使用 sqlGroupProjection() 方法。它有四个参数,第一个是查询的选择子句。在第一个参数中,您可以定义 count(1) 而不是 count(*)。例如:
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(EventLog.class, "event")
.createAlias("site", "site").setProjection(Projections.projectionList()
.add(Projections.sqlGroupProjection("var_val1,count(1) as count",
"var_val1",
new String[]{"var_val1", "count"},
new Type[]{Hibernate.STRING, Hibernate.INTEGER})))
.add(Restrictions.ge("event.date_time", strFrom))
.add(Restrictions.eq("site.companyID", custid))
于 2012-05-24T14:33:26.263 回答
1
您可以通过实现 StandartSQLFuction 接口来创建自定义计数函数。然后你必须继承你正在使用的方言类,并使用 registerFunction 方法在构造函数中注册你的函数。
于 2011-11-10T04:50:19.860 回答