gpt4 book ai didi

java - 将 HQL 子查询转换为 Criteria

转载 作者:行者123 更新时间:2023-11-30 09:46:03 24 4
gpt4 key购买 nike

我想翻译这个HQL的结构:

FROM Entity_1 obj
WHERE obj IN (FROM Entity2) OR
obj IN (FROM Entity3)

如何实现?

最佳答案

它在 Hibernate 标准文档中有解释:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html#querycriteria-detachedqueries

DetachedCriteria 也可用于表示子查询。涉及子查询的Criterion实例可以通过Subqueries或Property获取。

DetachedCriteria avgWeight = DetachedCriteria.forClass(Cat.class)
.setProjection( Property.forName("weight").avg() );
session.createCriteria(Cat.class)
.add( Property.forName("weight").gt(avgWeight) )
.list();

DetachedCriteria weights = DetachedCriteria.forClass(Cat.class)
.setProjection( Property.forName("weight") );
session.createCriteria(Cat.class)
.add( Subqueries.geAll("weight", weights) )
.list();

相关子查询也是可能的:

DetachedCriteria avgWeightForSex = DetachedCriteria.forClass(Cat.class, "cat2")
.setProjection( Property.forName("weight").avg() )
.add( Property.forName("cat2.sex").eqProperty("cat.sex") );
session.createCriteria(Cat.class, "cat")
.add( Property.forName("weight").gt(avgWeightForSex) )
.list();

关于java - 将 HQL 子查询转换为 Criteria,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7336979/

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