Hibernate 标准和计数列
我试图返回一个实体,该实体的列具有另一个一对多关系表的计数。我想使用 hibernate 标准而不是 HQL 来执行此操作。
select p.*, (select count(*) from child where child.parentid = p.id) as LEVELS
from parent p
I am trying to return an entity with a column that has the count of another table that is a one to many relation. I want to do this using hibernate criteria, not HQL.
select p.*, (select count(*) from child where child.parentid = p.id) as LEVELS
from parent p
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果父实体还包含子实体列表(双向关联),则可以使用条件返回子实体数量,如下所示:
If the parent entity also contains a list of children (bi-directional association), you can use criteria to return the count of children as follows:
做到这一点就可以了。不是很动态,但它会起作用。
Got it to work doing this. Not very dynamic but it will work.
定义一个对象字段映射,如下所示。然后,当您查询 Parent 对象时,每个对象应该有一个名为 Children 的列表类型字段,您可以在其上调用
size
。Define an object field mapping like below. Then when you query your Parent objects, each object should have a field of type list called children that you can call
size
on.你也许可以通过某种投影来做到这一点。
请参阅有关 Hibernate 的教程预测。您可能对
You might be able to do it by some kind of projection.
See the tutorial on Hibernate projections. You are probably interested in the
sqlProjection
method in the Projections class for the sub-query.