如何按条件中相关实体的属性进行分组?
我正在编写一个标准,该标准应按相关实体的属性对结果进行分组。我尝试过使用别名,尝试使用属性路径本身,但到目前为止我什么也没得到。 假设我的类是(粗略的草图):
class A{
@ManyToOne(...)
B b;
}
class B{
@OneToOne(...)
C c;
}
class C{
String s;
}
并且我想要一个标准,返回 C 中每个唯一字符串 s 的 A 和 B 的数量。
我最初的尝试是:
session.createCriteria(A.class)
.setProjection(Projections.projectionList()
.add(Projections.groupProperty("b.c.s"), "string")
.add(Projections.countDistinct("b"), "b's")
.add(Projections.rowCount(), "a's"))
这没有多大帮助,因为 bcs 不是 A 的属性。
然后我尝试
session.createCriteria(A.class)
.createAlias("b.c", "al")
.setProjection(Projections.projectionList()
.add(Projections.groupProperty("al.s"), "string")
.add(Projections.countDistinct("b"), "b's")
.add(Projections.rowCount(), "a's"))
将其实际上转换为 SQL,但没有走得太远,因为它在查询中不包含任何联接。
看来我在这里做错了什么。
是否可以使用 criteria API 获得此类高效查询?
I'm writing a criteria that should group the results by a property of a related entity. I've tried using an alias, tried using the property path itself, but so far I get nothing.
say my classes are (rough sketch):
class A{
@ManyToOne(...)
B b;
}
class B{
@OneToOne(...)
C c;
}
class C{
String s;
}
and I want a criteria that returns the amount of A's and B's for each unique string s in C.
my initial attempt was:
session.createCriteria(A.class)
.setProjection(Projections.projectionList()
.add(Projections.groupProperty("b.c.s"), "string")
.add(Projections.countDistinct("b"), "b's")
.add(Projections.rowCount(), "a's"))
This didn't help much as b.c.s is not a property of A.
then I tried
session.createCriteria(A.class)
.createAlias("b.c", "al")
.setProjection(Projections.projectionList()
.add(Projections.groupProperty("al.s"), "string")
.add(Projections.countDistinct("b"), "b's")
.add(Projections.rowCount(), "a's"))
this actually was translated to SQL, but did not get far, as it did not include any joins in the query.
seems I am doing something wrong here.
Is it possible to get an efficient query of this kind using the criteria API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许是这样的:
我认为你必须查看 double
createCriteria()
maybe something like this:
i think you must look in double
createCriteria()