Java 使用 JPA 注释自动从父项和父项中删除子项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23925322/
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-08-14 09:06:24  来源:igfitidea点击:

Delete child from parent and parent from child automatically with JPA annotations

javahibernatejpaormhibernate-mapping

提问by user1079877

Suppose that we have 3 Entities object class:

假设我们有 3 个实体对象类:

class Parent {
    String name;
    List<Child> children;
}

class Child {
    String name;
    Parent parent;
}

class Toy {
    String name;
    Child child;
}

How can I use JPA2.x (or hibernate) annotations to:

我如何使用 JPA2.x(或休眠)注释来:

  1. Delete all children automatically when parent delete (one to many)
  2. Delete child automatically from children list when it is deleted (many to one)
  3. Delete toy automatically when child remove (one to one)
  1. 父删除时自动删除所有子项(一对多)
  2. 删除子项时自动从子项列表中删除子项(多对一)
  3. 当孩子移除时自动删除玩具(一对一)

I'm using Hibernate 4.3.5 and mysql 5.1.30.

我正在使用 Hibernate 4.3.5 和 mysql 5.1.30。

Thanks

谢谢

采纳答案by Vlad Mihalcea

As explained in this article, the removeentity state transitionshould cascade from parent to children, not the other way around.

本文所述,remove实体状态转换应该从父级到子级级联,而不是相反。

You need something like this:

你需要这样的东西:

class Parent {

    String name;

    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    List<Child> children = new ArrayList<>();

    public void addChild(Child child) {
        child.setParent(this);
        children.add(child);
    }

    public void removeChild(Child child) {
        children.remove(child);
        child.setParent(null);
    }
}

class Child {

    String name;

    @ManyToOne
    Parent parent;

    @OneToOne(mappedBy = "child", cascade = CascadeType.ALL, orphanRemoval = true)
    Toy toy;
}

class Toy {
    String name;

    @OneToOne
    Child child;
}

回答by Masudul

You should use CascadeType.REMOVE. This is common annotation for both Hibernate and JPA. Hibernate has another similar type CacadeTypelike CascadeType.DELETE.

你应该使用CascadeType.REMOVE. 这是 Hibernate 和 JPA 的通用注释。Hibernate 有另一个类似的类型,CacadeTypeCascadeType.DELETE.

  1. Delete all children automatically when parent delete (one to many)

    class Parent {
      String name;
    
      @OneToMany(cascade = CascadeType.REMOVE)
      List<Child> children;
    }
    
  2. Delete child automatically from children list when it is deleted (many to one)

    class Child {
     String name;
     @ManyToOne(cascade = CascadeType.REMOVE)
     Parent parent;
    }
    
  3. Delete toy automatically when child remove (one to one)

    class Toy {
      String name;
      @OneToOne(cascade = CascadeType.REMOVE)
      Child child;
    }
    
  1. 父删除时自动删除所有子项(一对多)

    class Parent {
      String name;
    
      @OneToMany(cascade = CascadeType.REMOVE)
      List<Child> children;
    }
    
  2. 删除子项时自动从子项列表中删除子项(多对一)

    class Child {
     String name;
     @ManyToOne(cascade = CascadeType.REMOVE)
     Parent parent;
    }
    
  3. 当孩子移除时自动删除玩具(一对一)

    class Toy {
      String name;
      @OneToOne(cascade = CascadeType.REMOVE)
      Child child;
    }
    

回答by Adiya Buyantogtokh

orphanRemoval is delete all orphan entity example: store (s) has books(b1,b2,b3) and b1 has title(t) in this case if deleted store(s) some books(b2,b3) will be deleted. B2 and t still exist. if you use "cascade= CascadeType.Remove" just store(s) and all books will be deleted (only "t" exist).

orphanRemoval 是删除所有孤儿实体示例:商店(s)有书(b1,b2,b3)并且b1有标题(t)在这种情况下,如果删除了商店,一些书(b2,b3)将被删除。B2 和 t 仍然存在。如果您使用“ cascade= CascadeType.Remove”,则只使用store(s) 并且所有书籍都将被删除(仅存在“t”)。

s->b1,b2,b3 b2->t ------after(orphanRemoval = true)--------- b2->t

s->b1,b2,b3 b2->t ------ after(cascade=CascadeType.REMOVE)--------- t

If orphanRemoval=true is specified the disconnected entity instance is automatically removed. This is useful for cleaning up dependent objects that should not exist without a reference from an owner object.

如果指定 orphanRemoval=true 断开连接的实体实例将自动删除。这对于清理在没有所有者对象引用的情况下不应该存在的依赖对象很有用。

If only cascade=CascadeType.REMOVEis specified no automatic action is taken since disconnecting a relationship is not a remove operation.

如果仅cascade=CascadeType.REMOVE指定则不采取自动操作,因为断开关系不是删除操作。