Hibernate:加入标准

时间:2012-08-29 09:48:14

标签: java oracle hibernate

您好我需要使用Criteria

执行以下操作
Select * from product pd, (Select productID pd1 from ... where ...) tpd1, 
(Select productID pd2 from ... where ...) tpd2
where pd.productID = tpd1.pd1 and pd.productID = tpd1.pd2

我可以知道是否有可能吗?

原始SQL使用IN条件

Select * from product pd where productID in (Select productID pd1 from ... where ...) and 
productID in (Select productID pd2 from ... where ...) 

但是获取结果需要很长时间,使用连接SQL语句我能够更快地获得结果。

任何帮助?

1 个答案:

答案 0 :(得分:0)

鉴于你正在使用Hibernate,你可能不得不做这样的事情,如果预期的匹配数量相对较低,这应该可行:

select *
from   product pd
where  pd.productID in
   (select productID
    from   product pd2
    join   tpd1 on (pd2.productID = tpd1.pd1)
    join   tpd2 on (pd2.productID = tpd2.pd2)
    where  tpd1....
    and    tpd2....
   );

我假设product.productID上有一个唯一索引。

或者,您可以尝试使用EXISTS,这可能会或可能不会比原始查询更好:

select *
from   product pd
where  EXISTS
   (select null from tpd1 where pd.productID = tpd1.pd1 and ...)
and    EXISTS
   (select null from tpd2 where pd.productID = tpd2.pd2 and ...)
;
相关问题