gpt4 book ai didi

hql - 在 HQL 中对 3 个表使用左联接

转载 作者:行者123 更新时间:2023-12-04 01:35:19 27 4
gpt4 key购买 nike

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

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

此查询抛出异常.....

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”子句而不是 where...我在“on”或“with”上得到了相同的意外标记

异常 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)



我还尝试使用“with”子句而不是 where...我得到了相同的意外标记 on 或“with”

异常 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)



请帮忙。

最佳答案

我想您已经在配置中定义了所有需要的关联。如果是这样,在 HQL 中它看起来像这样:

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

HQL 中没有“ON”语句,休眠会根据您的映射和定义的关联自动执行。

关注 a.B 公元前 .

您根据已定义的别名 a & c 引用 B 和 C。

关于hql - 在 HQL 中对 3 个表使用左联接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12470876/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com