LEFT JOIN 中带有子句的 Hql 错误
Posted
技术标签:
【中文标题】LEFT JOIN 中带有子句的 Hql 错误【英文标题】:Hql error with-clause in LEFT JOIN 【发布时间】:2016-10-24 06:30:25 【问题描述】:经过几天的研究,我仍然没有找到解决方案。
我有这个抛出 querySyntaxException 的 HQL 请求:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: with-clause referenced two different from-clause elements [SELECT patDataset.patient.sId , patData ,proDataset.professional.sId, proData FROM com.inovelan.cloud.persistence.server.entities.DataSet patDataset INNER JOIN patDataset.patient patient with patient. sId in (:patientSidList) LEFT JOIN patient.patientProfessionals ppRole with ppRole.id.roleCodingOidcodesystem= :codeSystem AND ppRole.id.roleCodingCode=:codingCode LEFT JOIN ppRole.professional proRole LEFT JOIN proRole.dataSet proDataset with proDataset.archive =:archive LEFT JOIN proDataset.dataProfessionals proData with proData.itemDefinition.id.id in (:proItemDefs) INNER JOIN patDataset.dataSetDefinition patDatasetDef with patDatasetDef.id.id in(:patDatasetDefIdList) INNER JOIN patDataset.dataPatients patData LEFT JOIN patDataset.dataPatients data_patient_PAT_SEXE with ( data_patient_PAT_SEXE.itemDefinition.id.id=:data_patient_PAT_SEXE AND patDataset.dataSetDefinition.id.id ='DIRECTORY_PATIENT') LEFT JOIN patDataset.dataPatients data_patient_PAT_NOM_NAISSANCE with ( data_patient_PAT_NOM_NAISSANCE.itemDefinition.id.id=:data_patient_PAT_NOM_NAISSANCE AND patDataset.dataSetDefinition.id.id ='DIRECTORY_PATIENT') LEFT JOIN patDataset.dataPatients data_patient_PAT_PRENOM with ( data_patient_PAT_PRENOM.itemDefinition.id.id=:data_patient_PAT_PRENOM AND patDataset.dataSetDefinition.id.id ='DIRECTORY_PATIENT') WHERE patDataset.archive=:archive AND patient.active=:patientState AND ( patDatasetDef.id.id =:dataset_def_pat_DIRECTORY_PATIENT AND patData.itemDefinition.id.id in (:itemdef_list_pat_DIRECTORY_PATIENT) ) AND patDataset.date = (SELECT MAX(patDataset2.date) FROM com.inovelan.cloud.persistence.server.entities.DataSet patDataset2 WHERE patDataset2.dataSetDefinition.id.id = patDataset.dataSetDefinition.id.id AND patDataset2.patient.sId = patDataset.patient.sId) ORDER BY (CASE WHEN data_patient_PAT_SEXE.stringValue IS NULL then 1 ELSE 0 END), data_patient_PAT_SEXE.stringValue ASC,(CASE WHEN data_patient_PAT_NOM_NAISSANCE.stringValue IS NULL then 1 ELSE 0 END), data_patient_PAT_NOM_NAISSANCE.stringValue ASC,(CASE WHEN data_patient_PAT_PRENOM.stringValue IS NULL then 1 ELSE 0 END), data_patient_PAT_PRENOM.stringValue ASC]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:284)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:342)
我提取了请求以使其更易于理解,而这种“左连接”的原因是对元数据进行排序(它并不总是添加到请求中):
SELECT patDataset.patient.sId,
patData
FROM DataSet patDataset
INNER JOIN patDataset.patient patient
INNER JOIN patDataset.dataSetDefinition patDatasetDef WITH patDatasetDef.id.id IN(:patDatasetDefIdList)
LEFT JOIN patDataset.dataPatients data_patient_PAT_SEXE WITH (data_patient_PAT_SEXE.itemDefinition.id.id='SEXE'
AND patDatasetDef.id.id ='DIRECTORY_PATIENT')
WHERE patDataset.archive=:archive
AND patient.active=:patientState
ORDER BY (CASE
WHEN data_patient_PAT_SEXE.stringValue IS NULL THEN 1
ELSE 0
END), data_patient_PAT_SEXE.stringValue ASC
我已经在 SQL 中尝试过,并且我想做的工作完美
LEFT OUTER JOIN dataPatient datapatien1 ON dataset.sId=datapatien1.dataSetSid
AND (datapatien1.itemDefinitionId ='SEXE')
AND datasetdef.id ='DIRECTORY_PATIENT'
我在 hibernate https://hibernate.atlassian.net/browse/HHH-7321 中听说过这个问题 但我使用hibernate 5.2,应该没问题
我已经检查过类似的问题,但没有人有相同的情况
感谢您的帮助
【问题讨论】:
【参考方案1】:HQL 因“patDatasetDef.id.id”语句而产生此错误。解析此语句时出现问题。
相反,您必须对连接对象使用连接操作,如下所示: 选择母亲、后代、伴侣姓名 来自家猫当妈妈 内部加入mother.mate as mate left outer join mother.kittens as offspr
使用参考: For reference use this link
【讨论】:
以上是关于LEFT JOIN 中带有子句的 Hql 错误的主要内容,如果未能解决你的问题,请参考以下文章
如何使用“构造函数”在“选择子句”中为多个表的选定列编写HQL JOIN查询
为啥 MySQL LEFT JOIN 不返回所有行,除非有 WHERE 子句 - phpMyAdmin 问题