如何在JPA中为MySQL写一个自我加入
我有一个名为“相关”列的称为组的表; id,parent_id,activated_date 和 type 。 parent_id和activated_date是可根据的,如果在今天之前具有Activated_date,则认为该组被视为被激活,或者是类型类型的类型,或激活其父组。
id | parent_id | activated_date | 类型 |
---|---|---|---|
1 | 2020-01-01 | Typea | |
2 | 3 | typeb | |
3 | 2020-01-01 | Typec |
我正在尝试编写一个查询,该查询将列出所有激活的组,我遇到了一个非常令人困惑的问题。 (注意:我在使用案例语句,因为在JPA中,在某些条款中不允许使用条件表达式,但是它们在案例表达式内),
如果我的Where子句看起来像:
(CASE
WHEN
g.activated_date is not null AND
g.activated_date < CURDATE()
THEN TRUE
WHEN
g.group_type = 'typeA'
THEN TRUE
ELSE FALSE
END) = TRUE
然后我找到9000个激活的组(或1000个停用的组,如果我更改为错误的)。这是预期的,总共有10,000组。
但是,当我添加其他条件来检查父母的状态时(注意:这不需要递归,在此用例中,组不能具有“祖父母”):
(CASE
WHEN
g.activated_date is not null AND
g.activated_date < CURDATE()
THEN TRUE
WHEN
g.group_type = 'typeA'
THEN TRUE
WHEN
(g.parent.activated_date is not null AND g.parent.activated_date < CURDATE()) OR
(g.parent.group_type = 'typeA')
THEN TRUE
ELSE FALSE
END) = TRUE
然后,我突然发现我有8500个激活的组(并且750个停用) 。我无法解释缺少的750组;我在做什么显然做错了吗?
I have a table called groups with four [relevant] columns; id, parent_id, activated_date and type. parent_id and activated_date are nullable, and a group is considered to be activated if it has an activated_date prior to today, is of type typeA, or its parent group is activated.
id | parent_id | activated_date | type |
---|---|---|---|
1 | 2020-01-01 | typeA | |
2 | 3 | typeB | |
3 | 2020-01-01 | typeC |
I am trying to write a query that will list all of the activated groups, and I'm running into a very confusing issue. (Note: I am using CASE statements because in JPA conditional expressions are not allowed in SELECT clauses, but they are inside of CASE expressions)
If my WHERE clause looks like:
(CASE
WHEN
g.activated_date is not null AND
g.activated_date < CURDATE()
THEN TRUE
WHEN
g.group_type = 'typeA'
THEN TRUE
ELSE FALSE
END) = TRUE
Then I find 9000 activated groups (or 1000 deactivated groups if I change to FALSE). This is expected, there are a total of 10,000 groups.
BUT when I add an additional condition to check the parent's status (Note: this does not need to be recursive, groups cannot have 'grandparents' in this use case):
(CASE
WHEN
g.activated_date is not null AND
g.activated_date < CURDATE()
THEN TRUE
WHEN
g.group_type = 'typeA'
THEN TRUE
WHEN
(g.parent.activated_date is not null AND g.parent.activated_date < CURDATE()) OR
(g.parent.group_type = 'typeA')
THEN TRUE
ELSE FALSE
END) = TRUE
Then I am suddenly finding I have 8500 activated groups (and 750 deactivated). I cannot account for the missing 750 groups; am I doing something obviously wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的完整查询尚不清楚,但是如果“ G”实例没有父母,则将其从查询结果中删除。您需要使用此关系的左键将它们包括在结果中。
无论您在选择中使用g.parent的位置或您的子句所需的位置,都需要使用
parent
。所以Your full query isn't clear, but if 'g' instances don't have parents, they are removed from the query results. You would need to use a left join on this relationship to have them included in results.
Where ever you were using g.parent in your select or where clauses you then need to just use
parent
. So