HQL 错误:with 子句引用了两个不同的 from 子句元素

Posted

技术标签:

【中文标题】HQL 错误:with 子句引用了两个不同的 from 子句元素【英文标题】:HQL error: with-clause referenced two different from-clause elements 【发布时间】:2014-05-23 17:44:02 【问题描述】:

我开始使用 Hibernate,我正在使用 HQL,使用一些连接从数据库中检索数据,但收到此错误,感谢任何有关如何解决此问题的帮助。

Field.hbm.xml 文件:

    <id name="id" type="string">
        <column name="field_map_cd" />
        <generator class="assigned"></generator>
    </id>

Rule.hbm.xml 文件:

    <many-to-one name="src_field_map" column="field_map_cd" not-null="true" insert="false" update="false"/>
    <many-to-one name="tgt_field_map" column="field_map_cd" not-null="true" insert="false" update="false"/>

HQL 查询:

select t.id, t.name, t.src_field_map.id, s1.field_map_nm as src_field_map_nm, 
t.tgt_field_map.id,s2.field_map_nm as tgt_field_map_nm from Rule as t 
left join t.src_field_map as s1 left join t.tgt_field_map as s2 
with (s1.id = t.src_field_map.id and s2.id = t.tgt_field_map.id)

堆栈跟踪:

ERROR:  with-clause referenced two different from-clause elements
with-clause referenced two different from-clause elements
at org.hibernate.hql.internal.ast.HqlSqlWalker.handleWithFragment(HqlSqlWalker.java:465)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:413)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3858)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3644)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3522)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
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:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
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:1800)
at Utilities.test.readXrefRule(test.java:67)
at Utilities.test.main(test.java:171)
org.hibernate.hql.internal.ast.QuerySyntaxException: with-clause referenced 
two different from-clause elements [select t.id, t.name, t.src_field_map.id,
s1.field_map_nm as src_field_map_nm,t.tgt_field_map.id, s2.field_map_nm
as tgt_field_map_nm, orm.entity.OS_Rule as t left join t.src_field_map
as s1 left join t.tgt_field_map as s2 with (s1.id = t.src_field_map.id 
and s2.id = t.tgt_field_map.id )]
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:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
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:1800)
at Utilities.test.readRule(test.java:67)
at Utilities.test.main(test.java:171)

【问题讨论】:

【参考方案1】:

试试这个:

select 
    t.id, 
    t.name, 
    s1.id, 
    s1.field_map_nm as src_field_map_nm, 
    s2.id,
    s2.field_map_nm as tgt_field_map_nm
from Rule as t 
left join t.src_field_map as s1 
left join t.tgt_field_map as s2

我认为您不需要 with 子句,因为您只使用 FK,这无论如何都是隐式的。

您的with 子句实际上是两次引用同一个表ID:

s1.id = t.src_field_map.id

自从s1 = t.src_field_map

所以你的 with 子句翻译成:

t.src_field_map.id = t.src_field_map.id

因此,您可以删除 with 子句。

【讨论】:

遵循您的建议,并使用正确的字段修复了 Rule.hbm.xml 文件中的一对多对一列属性值,并且工作起来就像一个魅力。

以上是关于HQL 错误:with 子句引用了两个不同的 from 子句元素的主要内容,如果未能解决你的问题,请参考以下文章

LEFT JOIN 中带有子句的 Hql 错误

如何在现有的 Hql 或 SQL 查询中应用“With UR”子句?如何在我的查询为字符串格式的 Java 文件中附加它

with-clause 在 hSQL 中引用了两个不同的 from-clause 元素

Hibernate Hql 总结

WITH 子句可以在不使用 Select 语句的情况下具有硬编码值吗?

HQL 子选择错误