这是我需要Hibernate构建的SQL查询(在我的sql客户端工具中执行的运行速度非常快):
select decision.decisionid from Decision decision
INNER JOIN Proceeding proceeding on decision.proceedingId=proceeding.proceedingId
INNER JOIN IPRIGHT_PROCEEDING ipright on proceeding.proceedingId=ipright.proceedingId
where proceeding.dossierkindcode=7 and decision.creationdate>=TO_DATE('2017-04-22','YYYY-MM-DD') and decision.creationdate<=TO_DATE('2017-04-27','YYYY-MM-DD') or decision.updatedate>=TO_DATE('2017-04-22','YYYY-MM-DD') and decision.updatedate<=TO_DATE('2017-04-27','YYYY-MM-DD')
这是我写的HQL代码:
@Query( "select decision.decisionId FROM Decision decision, Proceeding proceeding, IprightProceeding iprightproceeding"
+ " INNER JOIN decision.proceeding"
+ " INNER JOIN iprightproceeding.proceeding"
+ " WHERE decision.proceeding.dossierKind = ?1"
+ " AND decision.creationDate>=?2"
+ " AND decision.creationDate<=?3"
+ " OR (decision.updatedate>=?2 AND decision.updatedate<=?3)"
这是生成的糟糕查询,需要花费很多时间甚至完全阻止服务器。
Hibernate: select decision0_.decisionid as col_0_0_ from DECISION decision0_
inner join PROCEEDING proceeding3_ on decision0_.proceedingid=proceeding3_.proceedingid
cross join PROCEEDING proceeding1_
cross join IPRIGHT_PROCEEDING iprightpro2_
inner join PROCEEDING proceeding4_ on iprightpro2_.proceedingid=proceeding4_.proceedingid
where proceeding3_.dossierkindcode=? and decision0_.creationdate>=? and decision0_.creationdate<=? or decision0_.updatedate>=? and decision0_.updatedate<=?
如您所见,它的活动加入了同一张桌子两次!!!
你能帮我吗?
由于
答案 0 :(得分:0)
首先,我认为您编写的是JPQL代码,而不是HQL,但无论如何您的查询都存在许多问题。首先,查看您的SELECT
:
select decision.decisionId
FROM Decision decision, Proceeding proceeding, IprightProceeding iprightproceeding
您正在这三个表之间进行交叉连接,因为实际上没有指定连接条件。您需要在JPQL的WHERE
子句中限制连接。如果不这样做,它就是一个交叉连接。
在此之上,您正在对相同的表执行一系列内部联接,从而导致某些表的第二次联接。尝试以下JPQL查询,这可能不完美,但应该更接近您打算运行的内容。
select d.decisionId FROM Decision d, Proceeding p, IprightProceeding i
where d.proceeding = p and
p.proceedingId = i and
d.proceeding.dossierKind = ?1 and
d.creationDate >= ?2 and
d.creationDate <= ?3 or
(d.updatedate >= ?2 AND d.updatedate <= ?3)
JPQL语法具有独特的风格,并且在很多方面与ANSI SQL不同。