Intersect在HQL中不起作用

时间:2014-10-07 04:58:31

标签: java sql sql-server hibernate hql

我正在使用Hibernate,java和Sql server来创建应用程序。在执行此操作时,我遇到了HQL不支持Intersect和Union的问题。对我来说,以下代码适用于本机SQL,但在处理HQL时它无法正常工作。

SQL

SELECT *
FROM LAB_TEST_SERVICES lts
WHERE lts.inttestid IN
(SELECT lsm.inttestid 
    FROM LAB_SPECIMEN_MAPPING lsm
    WHERE lsm.status = 1
intersect
SELECT ltl.inttestid 
    FROM LAB_TEST_LOCATION ltl
    WHERE ltl.status = 1)

HQL

String hql="FROM TEST_SERVICES_POJO lts WHERE lts.inttestid IN (SELECT lsm.inttestid FROM SPECIMEN_MAPPING_POJO lsm WHERE lsm.status = 1 intersect SELECT ltl.inttestid FROM TEST_LOCATION_POJO ltl WHERE ltl.status =1)";

任何人都可以帮我将SQL更改为HQL。提前谢谢。

1 个答案:

答案 0 :(得分:4)

您必须将INTERSECT指令替换为EXISTS关系中的两个AND条款。

试试这个:

SELECT *
FROM LAB_TEST_SERVICES_POJO lts
WHERE EXISTS
(SELECT lsm.inttestid 
    FROM LAB_SPECIMEN_MAPPING lsm
    WHERE lsm.status = 1
    AND lts.inttestid = lsm.inttestid)
AND EXISTS
(SELECT ltl.inttestid 
    FROM LAB_TEST_LOCATION ltl
    WHERE ltl.status = 1
    AND lts.inttestid = ltl.inttestid)