当我在 hql 中使用 join 关键字时,为什么会得到 Path Expected for join

2024-04-04

我有以下 HQL

String FIND_PRODUCT_CLASS_ID = "SELECT pc.id FROM ProductClass pc"+ 
" JOIN ProductGroup pg ON pc.id = pg.productClassId" +
" JOIN Product p ON pg.id = p.id" +
" JOIN ProductSub ps ON p.id = ps.productId WHERE ps.id =:childProductSubId";

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

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [SELECT pc.id FROM com.xxx.domain.ProductClass pc JOIN ProductGroup pg ON pc.id = pg.productClassId JOIN Product p ON pg.id = p.id JOIN ProductSub ps ON p.id = ps.productId WHERE ps.id =:childProductSubId]
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: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:1800)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
... 146 more

但是,如果我修改查询而不join如下关键字,就成功了。

String FIND_PRODUCT_CLASS_ID = "SELECT pc.id FROM ProductClass pc, ProductGroup pg, " +
 " Product p, ProductSub ps where pc.id = pg.productClassId "+
 " and pg.id = p.id and p.id = ps.productId and ps.id =:childProductSubId";

我知道我已经找到了解决方案,但我不确定为什么它不起作用joinHQL 中的关键字。有人可以向我解释一下吗?这与映射有关吗?在我的例子中,对象映射在 Hibernate 层中。


我们需要在 HQL 查询中提供路径。 这就是“加入的预期路径”即将出现的异常。

更改查询如下:请根据使用进行编辑

String FIND_PRODUCT_CLASS_ID = "SELECT pc.id FROM ProductClass pc"+ 
" JOIN pc.ProductGroup pg " +
" JOIN pg.Product p " +
" JOIN p.ProductSub ps WHERE ps.id =:childProductSubId";'

请参考this http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#queryhql-joins.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当我在 hql 中使用 join 关键字时,为什么会得到 Path Expected for join 的相关文章

随机推荐