Hibernate亲子建模

发布于 2024-08-23 20:44:10 字数 281 浏览 7 评论 0原文

我有一个模型课。这个类应该能够引用自身,即模型产生的“输出”应该像这样。

Some instanceOf Model.class
|-> Some instanceOf Model.class having parent instance referenced as parent_id
    |-> Some instanceOf Model.class having ...........

这些实例代表按层次结构组织的地理实体。老实说,我不知道如何实现这一点。

I have a Model class. This class should be able to reference itself, i.e. the resulting "output" from the model should be like this.

Some instanceOf Model.class
|-> Some instanceOf Model.class having parent instance referenced as parent_id
    |-> Some instanceOf Model.class having ...........

The instances represent geographical entities organized in a hierarchy. To be honest I have no idea how to implement this.

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

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

发布评论

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

评论(2

剪不断理还乱 2024-08-30 20:44:10
@Entity
class MyClass {

   @Id
   private Long id;

   @ManyToOne
   private MyClass parent;

   @OneToMany
   private Set<MyClass> children;

}

这是一个开始的地方。根据需要使用父级和/或子级,具体取决于您想要如何导航层次结构。我将让您填写详细信息。

@Entity
class MyClass {

   @Id
   private Long id;

   @ManyToOne
   private MyClass parent;

   @OneToMany
   private Set<MyClass> children;

}

Here's a place to start. Use parent and/or children as needed depending on how you want to navigate the hierarchy. I'll leave it to you to fill in the details.

手长情犹 2024-08-30 20:44:10

正如您的问题所示,您有@OneToOne 的

@Entity
public class SomeClass {

    @Id
    private Long id;

    @OneToOne
    private SomeClass relatedTo;

}

问候,

As shown in your question, you have a @OneToOne

@Entity
public class SomeClass {

    @Id
    private Long id;

    @OneToOne
    private SomeClass relatedTo;

}

regards,

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