HQL连接问题
对于以下问题,我编写sql没有问题。但是,我想将其写为 HQL 或 ICriteria。我正在使用 Fluent nhibernate 和最新的 nhibernate。情况:
ABCD AC AD 有 6 个班级。 B继承自A。AC代表A和C之间的am:m关系,AD代表A和D之间的am:m关系。让我们假设所有类都有一个ID列。我想计算与 B 相关的 C 和 D 的数量。
例如,A(和 B)中不存在 C 的 IList。这些类仍然是关联的...
这是一些代码(简化的):
public class A : Entity
{
}
public class B : A
{
}
public class C : Entity
{
}
public class D : Entity
{
}
public class AC : Entity
{
public virtual A A { get; set; }
public virtual C C { get; set; }
}
public class AD : Entity
{
public virtual A A { get; set; }
public virtual D D { get; set; }
}
在我的特定情况下,是否可以使用 HQL 和“左连接”(也显示具有零 C 和 D 的 B)?
谢谢。
Christian
PS:
我玩过一些 theta 风格的连接,但没有得到预期的结果,我不认为“左连接”在这里是可能的,不是吗?
PPS:
这种 theta 式连接类型有效,但前提是 B 与至少 1 个 C 和 D 相关联:
select
B.Id,
count(distinct AC.C.Id),
count(distinct AD.D.Id)
from AC AC, AD AD, B B
where AC.A.Id = B.Id and AD.A.Id = B.Id
group by B.Id
I have no problem writing sql for the following problem. However, I would like to write it as HQL or ICriteria. I am using fluent nhibernate and the latest nhibernate. The situation:
There are 6 classes A B C D AC AD. B inherits from A. AC represents a m:m relationship between A and C and AD represents a m:m relationship between A and D. Let us assume that all classes have an ID column. I would like to count the number of Cs and Ds B is associated with.
There is no IList of, for example, Cs in A (and B for that matter). Still the classes are associated ...
Here is some code (simplified):
public class A : Entity
{
}
public class B : A
{
}
public class C : Entity
{
}
public class D : Entity
{
}
public class AC : Entity
{
public virtual A A { get; set; }
public virtual C C { get; set; }
}
public class AD : Entity
{
public virtual A A { get; set; }
public virtual D D { get; set; }
}
Is this possible to use HQL and ‘left join’ (to also show the Bs that have zero Cs and Ds) in my particular case?
Thanks.
Christian
PS:
I have played a bit with theta style joins but did not get the expected results and I don’t think ‘left joins’ are possible here aren’t they?
PPS:
This theta-style join kind of works but only if B is assciated with at least 1 C and D:
select
B.Id,
count(distinct AC.C.Id),
count(distinct AD.D.Id)
from AC AC, AD AD, B B
where AC.A.Id = B.Id and AD.A.Id = B.Id
group by B.Id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用两个查询并对结果求和:
我看不到用单个查询来实现的方法。您的类似乎不是为这种查询而设计的...(这可能表明设计不合适,但并不需要如此。这取决于如何通常此查询适用于您的应用程序。)
相反,如果您拥有这些导航路径,而不询问关系类:
您将通过以下方式获得列表的大小:
或者在内存中更容易:-)
完全删除关系类,它可能如下所示:
You may use two queries and sum the result:
I can't see a way to make it with a single query. Your classes don't seem to be designed for that kind of query... (which may be a sign that the design isn't appropriate, but doesn't need to be. It depends of how typical this query is for your application.)
In contrast, if you had these navigation paths, without questioning the relation classes:
you would get the size of the lists by:
or even easier in memory :-)
Completely removing the relation classes, it could look like this: