Using Left Joins in HQL on 3 Tables

我的未来我决定 提交于 2019-12-31 21:42:07

问题


I have three tables A B and C. Now i want to execute this sql query in HQL:

select * from A as a 
left join 
B as b 
on 
a.id = b.id 
left join 
C as c 
on 
b.type=c.type;

Need help in writing equivalent HQL. I tried with this HQL...

Query q = session.createQuery(
    "FROM A as a 
     LEFT JOIN 
     B as b 
     on 
     a.id=b.id 
     LEFT JOIN 
     C as c 
     on 
     b.type=c.type");

This query is throwing exception .....

org.hibernate.hql.ast.QuerySyntaxError: unexpected token: LEFT near line 1, column 23 [FROM com.admin.A as a LEFT JOIN B as b where a.Id=b.Id LEFT JOIN C as c where b.type=c.type] at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:74) at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:214) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83) at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)

I also tried with "with" and "on" clauses instead of where...I get the same unexpected token on "on" or "with"

exception qith ON .....

org.hibernate.hql.ast.QuerySyntaxError: unexpected token: ON near line 1, column 41 [FROM com.admin.A as a LEFT JOIN B as b on a.Id=b.Id LEFT JOIN C as c onb.type=c.type] at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:74) at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:214) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83) at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)

I also tried with "with" clauses instead of where...I get the same unexpected token on or "with"

exception qith WITH .....

org.hibernate.hql.ast.QuerySyntaxError: unexpected token: ON near line 1, column 41 [FROM com.admin.A as a LEFT JOIN B as b on a.Id=b.Id LEFT JOIN C as c onb.type=c.type] at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:74) at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:214) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83) at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)

Please help.


回答1:


I suppose you've already defined all the needed associations in your configuration. If so, in HQL it would look like this:

from A as a left join a.B as b left join b.C as c 

There is no "ON" statement in HQL, hibernate does automatically based on your mappings and defined Associations.

Pay attention to a.B and b.C.

You refer to B and C based on already defined aliases a & c.




回答2:


Use this query

from A as a
left join fetch a.B as b 
left join fetch b.C as c 


来源:https://stackoverflow.com/questions/12470876/using-left-joins-in-hql-on-3-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!