我如何避免使用零零的自我引用列的N+

发布于 2025-02-03 17:07:17 字数 596 浏览 3 评论 0原文

我有一个奇怪的桌子看起来像这样。

ID            BIGINT NOT NULL AUTO INCREMENT
PARENT_ID     BIGINT NOT NULL DEFAULT '0'    

有两个问题。

  1. 没有FK。
  2. 联接列不是无效的,并将零用作未上限的状态。

我将@notfound用于第一个问题。

class MyEntity {


    @Id
    private Long id;

    @NotFound(action = IGNORE)
    @JoinColumn(name = "PARENT_ID")
    private MyEntity parent;
}

现在,当我尝试找到那些根元素时,每个元素应具有0 for parent.id.id

如何将0排除parent属性?

I have a weird table looks like this.

ID            BIGINT NOT NULL AUTO INCREMENT
PARENT_ID     BIGINT NOT NULL DEFAULT '0'    

There are two problems.

  1. There is no FK.
  2. The join column is not NULLABLE and uses zero as an unmapped status.

I used @NotFound for the first problem.

class MyEntity {


    @Id
    private Long id;

    @NotFound(action = IGNORE)
    @JoinColumn(name = "PARENT_ID")
    private MyEntity parent;
}

Now when I try to find those root elements, which each should has 0 for parent.id.

How can I exclude 0 for parent attribute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

耳根太软 2025-02-10 17:07:17

如果找不到该实体,则Hibernate将分配NULL。因此,要找到根部元素,请尝试:

select o from MyEntity o where o.parent is null

注意如果您使用notfoundaction.ignore始终会急切地获取协会。检查文档。

Hibernate assigns null if it can't find the entity. So to find the root elements try :

select o from MyEntity o where o.parent is null

Pay attention that if you use NotFoundAction.IGNORE the association is always fetched eagerly. Check the documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文