Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do I get Path expected for join when I use join key word in hql

Tags:

join

hibernate

I have the following 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";

When I run this query in Spring Hibernate environment, I get the following stack trace.

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

However, if I modifiy the query without join keyword like below, it's succeeded.

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";

I know that I have already found the solution but I'm not sure why it doesn't work with join key word in HQL. Can sombody explain this to me please? Is this something to do with the mapping? In my case objects are mapped in Hibernate layer.

like image 318
newday Avatar asked Apr 28 '17 04:04

newday


People also ask

Can we use join in HQL query?

HQL Join : HQL supports inner join, left outer join, right outer join and full join. For example, select e.name, a. city from Employee e INNER JOIN e.

What are the ways to express joins in HQL?

HQL supports two forms of association joining: implicit and explicit . The queries shown in the previous section all use the explicit form, that is, where the join keyword is explicitly used in the from clause. This is the recommended form. The implicit form does not use the join keyword.

How to join two entities in Hibernate?

Hibernate 5.1 Instead of referencing the attribute which defines the relationship between the two entities, you have to reference the second entity which you want to join and define the join criteria in the ON part of the statement. em. getTransaction(). commit();


2 Answers

We need to provide path in HQL query. That's the "Path expected for join" the exception is coming.

change query something like below: Please edit as per use

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";'

Please refer this.

like image 81
Ankur Singhal Avatar answered Oct 14 '22 02:10

Ankur Singhal


Since Hibernate 5.1 you can join unrelated entities using SQL like syntax joins.

In case you still get the antlr.SemanticException: Path expected for join! error you might have to use the full class name including the package e.g.

String FIND_PRODUCT_CLASS_ID = "SELECT pc.id FROM ProductClass pc"+ 
" JOIN com.mypackage.ProductGroup pg ON pc.id = pg.productClassId" +
" JOIN com.mypackage.Product p ON pg.id = p.id" +
" JOIN com.mypackage.ProductSub ps ON p.id = ps.productId WHERE ps.id =:childProductSubId";
like image 2
leventgo Avatar answered Oct 14 '22 04:10

leventgo