HQL 左连接不相关的实体 [英] HQL left join of un-related entities
问题描述
我有 2 个实体,A
和 B
.它们是相关的,但我不想将关系映射添加到 bean.
I have 2 entities, A
and B
. They are related but I do not want to add the relationship mapping to the beans.
我们如何使用 HQL 或标准在 A
和 B
之间使用左外连接?
How can we use left outer join between A
and B
using HQL or criteria?
对此有一些解决方法,
- 按照此处的说明使用本机 SQL.
- 添加关系并使用select a from A a left join a.b.
- 我们可以在 HQL 中进行内连接,如select * from A a, B b where a.some=b.some
- Use Native SQL as told here.
- Add a relationship and use select a from A a left join a.b.
- We can do a inner join in the HQL as select * from A a, B b where a.some=b.some
我总是回到这两个选项,有什么替代方法吗?或者这是不可能的?
I was always going back these 2 options, is there any alternative for this? Or this in not possible?
推荐答案
目前使用 HQL 在 where 子句中加入无关类的 theta 样式仅支持内连接.
Currently, the theta-style on joining the unrelated classes in the where clause using HQL only supports inner join.
在这种情况下支持外连接的请求是目前投票数第三的增强 但我不认为这个特性会在 Near 特性中实现,因为它需要 re-首先实现当前基于 ANTLER 的查询解析器,这在 IMO 看来是一项巨大的任务.
The request for supporting the outer join for such situation is currently the 3-rd most voted enhancement but I don't think this feature will be implemented in the near feature as it requires the re-implementation of the current ANTLER-based query parser first which seems to be a gigantic task IMO.
如果你坚持使用HQL进行左连接而不添加A和B之间的关系,你可以使用选项3先做内连接,然后使用下面的HQL
If you insist to use the HQL to perform left join without adding the relationship between A and B , you can use option 3 to do the inner join first, then use the following HQL
from A a where a.some not in ( select b.some from B)
找出所有不能加入B的A并以编程方式组合结果.
to find out all the A that cannot join B and combine the results programmatically .
更新
从 5.1.0 版开始 HHH-16(不相关类的显式连接) 已修复,我们应该能够加入不相关的实体.
As of release 5.1.0 HHH-16 (Explicit joins on unrelated classes) is fixed and we should be able to join the unrelated entities.
这篇关于HQL 左连接不相关的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!