Hibernate - 选择一对多的存在
在休眠状态下,我有实体 A 和 B。从 A 到 B 是一对多(A - < B)。
我需要编写一个查询,其中我可以选择 A (加上条件)加上一个附加列,该列反映每个 A 是否存在任何 B (在 B 上有一些附加条件)
因此可能有一个 A 没有 B另一个 A 带有 2 个 B。在本例中,我想要返回 2 行,每个 A 一行,第一行为“否”,第二行为“是”。我并不在意“不”和“是”实际上是什么,只是这样我可以区分。
如果我使用左外连接,在许多 B 的情况下,每个 A 会得到不止一行。
我怎样才能在 HQL 中写这个?我是否需要一个分组依据(但我认为你不能按实体分组)?
附加:我还需要根据某些条件限制相关的 B。
In hibernate I have entities A and B. There's is a one-to-many from A to B (A -< B).
I need to write a query where I can select A (plus criteria) plus an additional column that reflects the existence, or not, of any B for each A (with some additional criteria on B)
So there might be one A with no Bs and another A with 2 Bs. In this case I want 2 rows back, one for each A with "no" for the first and "yes" for the second. I am not fussed what "no" and "yes" actually are, just so I can distinguish.
If I use a left outer join I will get more than one row per A in the many B case.
How I can I write this in HQL? Do I perhaps need a group by (but you can't group by entities I think)?
Additional: I also need to restrict the Bs that are relevant based on some conditions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种可能的解决方案是使用
case
子句:也可以使用
join
和group by
,但是,正如您所注意到的,这取决于您的 DBMS您可能需要在group by
子句中枚举实体的所有属性。One possible solution is to use
case
clause:join
withgroup by
can be used as well, but, as you noticed, depending on your DBMS you may need to enumerate all properties of your entity ingroup by
clause.