在3个表的HQL中使用左联接 [英] Using Left Joins in HQL on 3 Tables

查看:181
本文介绍了在3个表的HQL中使用左联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表A B和C.现在我想在HQL中执行此sql查询:

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;

在编写等效的HQL时需要帮助.我尝试过此HQL ...

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:意外令牌:LEFT附近 第1行,第23列[从FROM com.admin.A作为左联接B作为b,其中 a.Id = b.Id左联接C为c,其中b.type = c.type]在 org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:74) 在 org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:214) 在 org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127) 在 org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83) 在 org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)

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)

我还尝试了"with"和"on"子句,而不是在哪里...我在"on"或"with"上得到了相同的意外标记

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

例外qith开启.....

exception qith ON .....

org.hibernate.hql.ast.QuerySyntaxError:意外令牌:在线附近接通 1,第41列[FROM com.admin.A作为LEFT JOIN B如a.Id = b.Id LEFT上的b 在以下位置加入C作为c onb.type = c.type] org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:74) 在 org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:214) 在 org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127) 在 org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83) 在 org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)

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)

我也尝试了"with"子句,而不是在哪里...我在"with"或"with"上得到了相同的意外标记

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

例外qith与.....

exception qith WITH .....

org.hibernate.hql.ast.QuerySyntaxError:意外令牌:在线附近接通 1,第41列[FROM com.admin.A作为LEFT JOIN B如a.Id = b.Id LEFT上的b 在以下位置加入C作为c onb.type = c.type] org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:74) 在 org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:214) 在 org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127) 在 org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83) 在 org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)

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)

请帮助.

推荐答案

我想您已经在配置中定义了所有需要的关联.如果是这样,则在HQL中它将如下所示:

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 

HQL中没有"ON"语句,hibernate会根据您的映射和定义的关联自动执行.

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

请注意 a.B b.C .

您基于已经定义的别名a&来引用B和C. c.

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

这篇关于在3个表的HQL中使用左联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆