java hql左连接写法,在3个表的HQL中使用左联接

博主在尝试使用Hibernate HQL进行多表左连接(A左Join B on a.id = b.id, A左Join C on b.type = c.type)时遇到错误,寻求如何正确书写并解决异常。HQL中无ON声明,需确保已定义好关联映射。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值