【问题标题】:Path expected for join HQL - Hibernate加入 HQL 的预期路径 - Hibernate
【发布时间】:2017-10-23 13:47:31
【问题描述】:

我有以下 HQL

.createQuery("select dl from DocumentLink dl " +
                    "left join DigitalFile df on df.iddigitalfile = dl.parentId and dl.entity.identity =:entityId " +
                    "where (dl.entity.identity =:entityId && dl.parentId =: parentId) || " +
                    "(df.entity.identity =:entityId and df.parentId =: parentId) ", DocumentLink.class);

所需的 SQL 查询:

select * from documentlink dl
        left join digitalfile df on df.iddigitalfile = dl.ParentId and dl.entity = 261
        where (dl.entity = 5 and dl.parentId = 39845) OR (df.entity = 5 and df.parentId = 39845);

当我在 Hibernate 环境中运行此查询时,我得到了以下堆栈跟踪。

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [select dl from com.raetsmarine.raetsbase3.domain.DocumentLink dl left join DigitalFile df on df.iddigitalfile = dl.parentId and dl.entity.identity =:entityId where (dl.entity.identity =:entityId and dl.parentId =: parentId)]
    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)

你有什么建议我怎样才能以适当的方式转换它?

【问题讨论】:

  • 通过阅读文档了解联接如何在 HQL 中工作? docs.jboss.org/hibernate/orm/5.2/userguide/html_single/…
  • 为什么在join中需要on子句?
  • @JimmyB 我使用了关键字“with”而不是“on”,但我得到了同样的错误。
  • 我的意思是:你为什么需要指定连接条件? JPA 应该能够为您做这件事,除非您有一些 JPA 不知道的特殊连接标准。
  • 顺便说一句,and dl.entity.identity =:entityId 在连接中没有意义。

标签: java sql hibernate hql


【解决方案1】:

假设有两个类

@Getters
@Setters
public class A {
    Long id;

    @OneToOne
    B b
}

@Getters
@Setters
public class B {
    Long id;
    Long someAttribute;
}

然后加入 HQL 会是这样的

SELECT a FROM A a
LEFT OUTER JOIN a.b
WHERE b.someAttribute = 'Any Value'

请注意,类的名称已被省略,而是在 HQL 中使用了字段的名称,而实际上并未提供 ON clause

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多