gpt4 book ai didi

java - 我们可以在 hibernate 条件查询中包含条件吗?

转载 作者:行者123 更新时间:2023-11-29 19:49:59 25 4
gpt4 key购买 nike

我正在使用 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 '%%';

请帮助我解决上述问题。

最佳答案

我不认为 HQL 可以在 from 子句中执行子查询

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

请注意,HQL 子查询只能出现在 select 或 where 子句中。我想您可以将其更改为 native 查询并以这种方式执行。

关于java - 我们可以在 hibernate 条件查询中包含条件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40844737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com