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.
解决方案
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.