Java 如何在where子句中使用子查询?
我们希望在查询中集成计算字段。 该计算字段通过从表2中选择最大值(x)获得 但是在我们的主查询中,如果我们想对这个计算字段进行过滤,我们必须将它集成到where子句中 我们的问题是将子查询集成到Java 如何在where子句中使用子查询?,java,hibernate,subquery,criteria,Java,Hibernate,Subquery,Criteria,我们希望在查询中集成计算字段。 该计算字段通过从表2中选择最大值(x)获得 但是在我们的主查询中,如果我们想对这个计算字段进行过滤,我们必须将它集成到where子句中 我们的问题是将子查询集成到中,其中…处于条件 我们尝试使用子查询。在函数中,问题是我们无法插入值列表 这里是我们的SQL请求 select this_.DMDE_CEE_ID as y0_, this_.DT_DMDE as y1_, (select max(this0__.STATUT)
中,其中…处于条件
我们尝试使用子查询。在
函数中,问题是我们无法插入值列表
这里是我们的SQL请求
select
this_.DMDE_CEE_ID as y0_,
this_.DT_DMDE as y1_,
(select
max(this0__.STATUT) as y0_
from
CS_FC_DMDE_CEE this0__
where
this0__.DMDE_CEE_ID=this_.DMDE_CEE_ID limit 1 ) as y16_
from
xxx this_
where
( select
max(controleDemandeCee_.STATUT) as y0_
from
CS_FC_DMDE_CEE controleDemandeCee_
where
controleDemandeCee_.DMDE_CEE_ID=this_.DMDE_CEE_ID
) IN (3,5)
order by
this_.NOM_DOS asc,
this_.DT_DMDE asc limit 10;
在这里,我们可以得到最好的结果:
select
this_.DMDE_CEE_ID as y0_,
this_.DT_DMDE as y1_,
(select
max(this0__.STATUT) as y0_
from
CS_FC_DMDE_CEE this0__
where
this0__.DMDE_CEE_ID=this_.DMDE_CEE_ID limit 1 ) as y16_
from
xxx this_
where
5 IN ( select
max(controleDemandeCee_.STATUT) as y0_
from
CS_FC_DMDE_CEE controleDemandeCee_
where
controleDemandeCee_.DMDE_CEE_ID=this_.DMDE_CEE_ID
)
order by
this_.NOM_DOS asc,
this_.DT_DMDE asc limit 10;
包含条件和子查询的Java代码:
final DetachedCriteria dControleDemande = DetachedCriteria.forClass(ControleDemandeBean.class, CONTROLE);
dControleDemande.add(Restrictions.eqProperty(CONTROLE_DEMANDE_ID, DMD_ID));
dControleDemande.setProjection(Projections.max(CONTROLE + ".statut.id"));
cDmd.add(Subqueries.in(5L, dControleDemandeCee))
我们不能用长的列表/数组替换5L
有什么想法吗?
谢谢
注意:我们必须使用标准。客户端拒绝本机HQL或SQL。您可以实现一个自定义的标准,该标准
( select
max(controleDemandeCee_.STATUT) as y0_
from
CS_FC_DMDE_CEE controleDemandeCee_
where
controleDemandeCee_.DMDE_CEE_ID=this_.DMDE_CEE_ID
) IN (5)