子查询中的IN子句的Hibernate Criteria等效项? [英] Hibernate Criteria equivalent for IN clause in Subqueries?

查看:94
本文介绍了子查询中的IN子句的Hibernate Criteria等效项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想翻译一个这样的查询:

I would like to translate a query like this one:

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

要休眠标准表单,官方 Hibernate的文档不是足够的,因为它没有说明如何应用 IN 语句。

To hibernate Criteria form, and the official documentation of Hibernate is not enough because it doesn't say how to apply the IN statement.

任何提示?

推荐答案

条件API没有规定添加另一个查询作为限制..我想@Niroshan Abayakoon试图说的是,你需要执行 IN 子句的单独和查询将结果添加到 Restrictions.in()条件。

The criteria API does not have a provision to add another query as a restriction.. i think what @Niroshan Abayakoon was trying to say is that you need to execute the queries for the IN clause seperatly & add the result to the Restrictions.in() condition.

List<?> entity2Data=//get data from either a query or criteria
List<?> entity3Data=//get data from either a query or criteria
Criteria c = // obtain criteria from session
// basically creates an OR condition chain
Disjunction orConditions = Restrctions.disjunction();
orConditions.add(Restrictions.in("obj", entity2Data));
orConditions.add(Restrictions.in("obj", entity3Data));
c.add(orConditions); 

这将使hibernate考虑 IN 子句。

this would get hibernate to consider the list within the IN clause.

在这种情况下,总是更好地回到HQL。

Its always better to fallback to HQL in situations like this.

这篇关于子查询中的IN子句的Hibernate Criteria等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆