如何在JPA中为MySQL写一个自我加入

发布于 2025-01-26 08:13:38 字数 1381 浏览 6 评论 0原文

我有一个名为“相关”列的称为组的表; id,parent_id,activated_date type 。 parent_id和activated_date是可根据的,如果在今天之前具有Activated_date,则认为该组被视为被激活,或者是类型类型的类型,或激活其父组。

idparent_idactivated_date类型
12020-01-01Typea
23typeb
32020-01-01Typec

我正在尝试编写一个查询,该查询将列出所有激活的组,我遇到了一个非常令人困惑的问题。 (注意:我在使用案例语句,因为在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.

idparent_idactivated_datetype
12020-01-01typeA
23typeB
32020-01-01typeC

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身情人 2025-02-02 08:13:39

您的完整查询尚不清楚,但是如果“ G”实例没有父母,则将其从查询结果中删除。您需要使用此关系的左键将它们包括在结果中。

"select <your select clause> where G g left join g.parent parent where <your Where clause>"

无论您在选择中使用g.parent的位置或您的子句所需的位置,都需要使用parent。所以

(CASE
  WHEN
    g.activated_date is not null AND
    g.activated_date < CURDATE()
    THEN TRUE
  WHEN
    g.group_type = 'typeA'
    THEN TRUE
  WHEN
    parent is not null AND
    ((parent.activated_date is not null AND parent.activated_date < CURDATE()) OR
    (parent.group_type = 'typeA'))
    THEN TRUE
  ELSE FALSE
END) = TRUE

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.

"select <your select clause> where G g left join g.parent parent where <your Where clause>"

Where ever you were using g.parent in your select or where clauses you then need to just use parent. So

(CASE
  WHEN
    g.activated_date is not null AND
    g.activated_date < CURDATE()
    THEN TRUE
  WHEN
    g.group_type = 'typeA'
    THEN TRUE
  WHEN
    parent is not null AND
    ((parent.activated_date is not null AND parent.activated_date < CURDATE()) OR
    (parent.group_type = 'typeA'))
    THEN TRUE
  ELSE FALSE
END) = TRUE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文