我们可以在hibernate中的条件查询中有标准吗?

时间:2016-11-28 12:47:46

标签: java mysql hibernate

我正在使用hibernate,在哪里获取数据我正在使用标准。但我认为使用标准有一些限制。但是我想在下面问一下我认为可以通过在休眠中使用标准来实现的事情。

如何在标准库hibernate中编写以下查询

select * from (select d.priceId , sum(d.price) as sumP, avg(d.price) as  avgP from data d group by d.priceId) as adata where sumP like '%%';

至于现在我可以为下面的SQL写标准基础hibernate查询:

select d.priceId , sum(d.price) as sumP, avg(d.price) as  avgP from data d group by d.priceId

如下

Session session = sessionFactory.getCurrentSession();
criteria = session.createCriteria(Data.class, "data")
criteria.setProjection(Projections.projectionList()
.add(Projections.groupProperty("d.priceId"))
.add(Projections.sqlProjection("sum(d.price) as sumP", new String[] {"sumP"},new org.hibernate.type.BigDecimalType[] {new org.hibernate.type.BigDecimalType()}), "sumP")
.add(Projections.sqlProjection("avg(d.price) as avgP", new String[] {"avgP"},new org.hibernate.type.BigDecimalType[] {new org.hibernate.type.BigDecimalType()}), "avgP"));

然后如何为下面的SQL编写基于标准的查询:

 select * from (select d.priceId , sum(d.price) as sumP, avg(d.price) as  avgP from data d group by d.priceId) as adata where sumP like '%%';

请在上面帮助我。

1 个答案:

答案 0 :(得分:0)

我不认为HQL可以在from子句

中执行子查询

https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch16.html#queryhql-subqueries

请注意,HQL子查询只能出现在select或where子句中。 我想你可以把它改成原生查询并以那种方式执行。