如何使用 HQL 选择 @OneToMany(mappedBy)) 集合的大小
首先是一些上下文和解释:
我编写了以下 HQL 查询:
select size(childGroups), size(ipts), g.id, g.name, g.status from LogicalGroup g where g.isRoot=true group by g.id, g.name, g.status
LogicalGroup
是一个具有标量字段的实体:id、name、status、isRoot
和两个实体集合字段:
- < code>childGroups
这是一个
@OneToMany(mappedBy = "parentGroup")
Set
parentGroup
是 LogicalGroup
中的另一个字段。 (每个组包含一组子组和一个父组)。
和
ipts
,
它是一个
@OneToMany(mappedBy = "logicGroup")
设置
每个IPT
都有一个字段逻辑组
表示它所属的组。
我希望查询执行的操作是选择 childGroups
集合的大小以及属于根 LogicalGroup
的 ipts
集合的大小(其中isRoot
==true)。只有一个组 isRoot
==true。
逻辑组和 IPT 从超类 AbstractEntity
继承其 id
字段。表结构是这样的:每个类有一个表(超类也有自己的表)。父/子逻辑组或逻辑组/IPT 之间的关系没有连接表。
现在的问题是:
首先,我应该说查询正确返回标量值。
问题是查询似乎返回了正确设置的LogicalGroup
的大小,但似乎返回了IPT
的总数IPT
表,而不仅仅是属于 LogicalGroup
的表。我哪里出错了?查询或集合的映射是否有问题?
我打开 SQL 日志记录,Hibernate 生成并执行以下内容:
Hibernate: select count(childgroup1_.parentGroup_id) as col_0_0_,
count(ipts2_.logicalGroup_id) as col_1_0_,
logicalgro0_.id as col_2_0_,
logicalgro0_.name >as col_3_0_,
logicalgro0_.status as col_4_0_ from LogicalGroup logicalgro0_
inner join AbstractEntity
logicalgro0_1_ on logicalgro0_.id=logicalgro0_1_.id,
LogicalGroup childgroup1_,
IPT ipts2_
where
logicalgro0_.id=childgroup1_.parentGroup_id and
logicalgro0_.id=ipts2_.logicalGroup_id and
logicalgro0_.isRoot=true
group by
logicalgro0_.id , logicalgro0_.name , logicalgro0_.status
First some context and explanation:
I have written the following HQL query:
select size(childGroups), size(ipts), g.id, g.name, g.status from LogicalGroup g where g.isRoot=true group by g.id, g.name, g.status
LogicalGroup
is an entity with scalar fields: id, name, status, isRoot
and two entity collection fields:
childGroups
which is a
@OneToMany(mappedBy = "parentGroup")
Set<LogicalGroup>
parentGroup
is another field in LogicalGroup
. (Each group contains a set of child groups and a parent group).
and
ipts
which is a
@OneToMany(mappedBy = "logicalGroup")
Set<IPT>
each IPT
has a field logicalGroup
denoting the group that it belongs to.
What I would like the query to do, is select the size of the childGroups
collection and the size of the ipts
collection belonging to the root LogicalGroup
(where isRoot
==true). There is only ever one group for which isRoot
==true.
Logical Group and IPT inherit their id
fields from a superclass AbstractEntity
. The table structure is such that there is one table per class, (the super class also has its own table). There are no join tables for the relationships between parent/child logical groups or logical group/IPT.
Now the question:
Firstly, I should say that the query returns the scalar values correctly.
The problem is that the query appears to return the size of the LogicalGroup
set correctly, but seems to be returning the total number of IPT
s in the IPT
table, rather than just the ones belonging to the LogicalGroup
. Where am I going wrong? Is something wrong with the query or potentially the mapping of the collections?
I turned on SQL logging and the following is generated and executed by Hibernate:
Hibernate: select count(childgroup1_.parentGroup_id) as col_0_0_,
count(ipts2_.logicalGroup_id) as col_1_0_,
logicalgro0_.id as col_2_0_,
logicalgro0_.name >as col_3_0_,
logicalgro0_.status as col_4_0_ from LogicalGroup logicalgro0_
inner join AbstractEntity
logicalgro0_1_ on logicalgro0_.id=logicalgro0_1_.id,
LogicalGroup childgroup1_,
IPT ipts2_
where
logicalgro0_.id=childgroup1_.parentGroup_id and
logicalgro0_.id=ipts2_.logicalGroup_id and
logicalgro0_.isRoot=true
group by
logicalgro0_.id , logicalgro0_.name , logicalgro0_.status
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论