为什么此查询不起作用
FROM WorkflowConfiguration
WHERE ((saReplacement IS NOT NULL) AND (:currentTime >= saReplacement.start) AND (saReplacement.end >= :currentTime)) OR
((hrmsAdminReplacement IS NOT NULL) AND (:currentTime >= hrmsAdminReplacement.start) AND (hrmsAdminReplacement.end >= :currentTime)) OR
((hrManagerReplacement IS NOT NULL) AND (:currentTime >= hrManagerReplacement.start) AND (hrManagerReplacement.end >= :currentTime)) OR
((payrollAdminReplacement IS NOT NULL) AND (:currentTime >= payrollAdminReplacement.start) AND (payrollAdminReplacement.end >= :currentTime))
但是这个确实
FROM WorkflowConfiguration
WHERE (saReplacement IN (from WorkflowReplacement as replacement WHERE (:currentTime >= replacement.start) AND (replacement.end >= :currentTime))) OR
(hrmsAdminReplacement IN (from WorkflowReplacement as replacement WHERE (:currentTime >= replacement.start) AND (replacement.end >= :currentTime))) OR
(hrManagerReplacement IN (from WorkflowReplacement as replacement WHERE (:currentTime >= replacement.start) AND (replacement.end >= :currentTime))) OR
(payrollAdminReplacement IN (from WorkflowReplacement as replacement WHERE (:currentTime >= replacement.start) AND (replacement.end >= :currentTime)))
saReplacement,hrmsAdminReplacement,hrManagerReplacement和payrollAdminReplacement这么长的原因都可以为null,所以我进行检查以确保它们不为null,因此子句的其余部分不会被执行。但由于某种原因,当我运行第一个查询时,我没有得到任何结果,但是如果我运行第二个(很少有效),我得到了所有预期的结果。这是Hibernate的HQL中的错误还是我失去了理智?
答案 0 :(得分:1)
你有没有试过这样的事情:
FROM WorkflowConfiguration wfc
LEFT JOIN FETCH wft.saReplacement a
LEFT JOIN FETCH wft.hrmsAdminReplacement b
LEFT JOIN FETCH wft.hrManagerReplacement c
LEFT JOIN FETCH wft.payrollAdminReplacement d
WHERE
((:currentTime >= a.start) AND (a.end >= :currentTime)) or
((:currentTime >= b.start) AND (b.end >= :currentTime)) or
((:currentTime >= c.start) AND (c.end >= :currentTime)) or
((:currentTime >= d.start) AND (d.end >= :currentTime))