0
@Entity
public class Parent {
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
int id;
@OneToMany(cascade=CascadeType.REMOVE)
List<Item> children = new ArrayList<Child>();
}
@Entity
public class Child {
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
int id;
}
正如您在上面看到的,我有一个父对象与子对象之间的OneToMany关系。如果我删除父项的一个实例,则所有的子项也将被删除。有没有一种方法可以让它反过来工作?JPA:反向级联删除
Parent p = new Parent();
Child c = new Child();
p.children.add(c);
EntityManager.persist(p);
EntityManager.persist(c);
EntityManager.remove (c);
此代码无一例外地运行,但是当下次加载p时,会附加一个新的子代。