java 休眠同表父/子关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30334044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:55:10  来源:igfitidea点击:

Hibernate same table parent / child relation

javahibernate

提问by antohoho

I have Parent/Child relationship in a single-table, let's say as simple as

我在单表中有父/子关系,让我们说简单

id | parent_id | some_data

I am trying to understand / implement best practices how to build Hibernate classes for single table relationship.

我正在尝试了解/实现如何为单表关系构建 Hibernate 类的最佳实践。

I have a feed that comes from somewhere and it has nested JSON structure, so after parse it I want to have it representation in OracleDB.

我有一个来自某个地方的提要,它有嵌套的 JSON 结构,所以在解析它之后我想在 OracleDB 中让它表示。

Feed looks like

饲料看起来像

{
   1:
       => 2:
              => 3
              => 4:
                      => 5
       => 6  
}

this data should end up in db as :

此数据应以 db 结尾:

1 | 0 (main rec, no parent)
2 | 1
3 | 2
4 | 2
5 | 4
6 | 1 

And it could go deeper and deeper...

它可能会越来越深......

I would like to traverse JSON structure and build classes that at the end I save in db

我想遍历 JSON 结构并构建最后保存在 db 中的类

session.save(parent)

parent would be instance of my hibernate mapped class, lets name it Feed.

parent 将是我的休眠映射类的实例,我们将其命名为 Feed。

Each time I descend a tree, it creates a new Feed, finds it's parent and add it to the list.

每次我下树时,它都会创建一个新的 Feed,找到它的父级并将其添加到列表中。

Feed **parent** = new Feed();

  ->   Feed child2 = new Feed();
       child2.setParent(parent)
       parent.add(child2); 

      ->   Feed child3 = new Feed();
           child3.setParent(child2)
           child2.add(child3);

           Feed child4 = new Feed();
           child4.setParent(child2)
           child2.add(child4);

............. 

session.save(**parent**)

My question is could I use @ManyToOne and @OneToMany approach?

我的问题是我可以使用 @ManyToOne 和 @OneToMany 方法吗?

I also looked at @Inheritance(strategy = InheritanceType.SINGLE_TABLE) but I do not think I could apply it to my problem since I do not know parents, they are dynamic. I could not build parent class and extend it.

我还查看了 @Inheritance(strategy = InheritanceType.SINGLE_TABLE) 但我不认为我可以将它应用于我的问题,因为我不认识父母,他们是动态的。我无法构建父类并扩展它。

So bottom line I am trying to solve two problems

所以底线我试图解决两个问题

  1. Easy save on a parent level

  2. How to get data from a parent to a leaf when I need it

  1. 轻松保存在父级

  2. 如何在需要时从父级获取数据到叶子

Some Hibernate Mapped Classes example would be very appreciated.

一些 Hibernate Mapped Classes 示例将不胜感激。

回答by antohoho

Seems like all I needed to do is:

似乎我需要做的就是:

@Entity
@Table(name = "table_name")
public class TableName{

......

@Transient
private Long parentId;

...... 

@ManyToOne(fetch = FetchType.LAZY, optional=true)
@JoinColumn(name="parent_id")
private TableName parent;

@OneToMany(mappedBy="parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval=true)
private Set<TableName> children = new HashSet<TableName>();

Than simply

比简单的

TableName papa = new TableName();
papa.setParent(null); // parent

    TableName kid1 = new TableName();
    kid1.setParent(papa);
    papa.getChildren().add(kid1);

        TableName kid1_1 = new TableName();
        kid1_1.setParent(kid1);
        kid1.getChildren().add(kid1_1);

    TableName kid2 = new TableName();
    kid2.setParent(papa);
    papa.getChildren().add(kid2);


session.save(papa)

回答by anirban

Why don't you create a self referencing one to many relation keeping cascade = all. So individual child objects need not be saved separately. Only saving parent is enough. For example.

为什么不创建一个自引用的一对多关系保持级联 = 全部。因此不需要单独保存单个子对象。只有保存父母就足够了。例如。

Adding a one to many relationship to a self reference parent/child

向自引用父/子添加一对多关系