简体   繁体   English

Hibernate Criteria等效于子查询中的IN子句?

[英]Hibernate Criteria equivalent for IN clause in Subqueries?

I would like to translate a query like this one: 我想翻译像这样的查询:

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

To hibernate Criteria form, and the official documentation of Hibernate is not enough because it doesn't say how to apply the IN statement. 要暂停Criteria表单,而Hibernate的官方文档是不够的,因为它没有说明如何应用IN语句。

Any hint? 任何提示?

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. 标准API没有规定添加另一个查询作为限制..我认为@Niroshan Abayakoon试图说的是你需要单独执行IN子句的查询并将结果添加到Restrictions.in()条件。

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); 

this would get hibernate to consider the list within the IN clause. 这将使hibernate考虑IN子句中的列表。

Its always better to fallback to HQL in situations like this. 在这种情况下,最好还是回退到HQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM