当使用继承和ManyToOne与JPA的关系时如何级联删除 [英] How to cascade delete when using inheritance and ManyToOne relationship with JPA
问题描述
我正在用spring实现一个简单的用户管理系统。为了实现更好的模型结构,我使用继承。该系统建立在三个班级上。包含一般信息的 userdata
类。继承并扩展用户类的父类
类,最后是包含父对象的子类
类。
我想要做的是通过删除父
来删除所有的孩子(被称为父母)对象。
我已经能够通过我的视图在mySQL-DB上保存/删除对象,因此存储库/服务/控制器似乎可以工作。 / b>
为了让您更好地理解我的情况,我在下面加了一些代码。
我的用户
类:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class UserData实现了Persistable< String> {
private static final long serialVersionUID = 1L;
@Id
@NotNull
私人字符串用户名;
@NotNull
私人字符串密码;
@NotNull
private String firstName;
@NotNull
私人字符串姓氏;
我的父母
继承自User的类和持有一组Child:
@Entity
公共类父扩展用户数据{
private static final long serialVersionUID = 1L;
私人字符串imgName;
@OneToMany(fetch = FetchType.EAGER)
@ElementCollection
私人设置< Child>儿童;
我的 Child
@Entity
@Transactional
public class Child实现Persistable< Long> {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
私有长ID;
@NotNull
private String firstName;
@NotNull
私人字符串姓氏;
@NotNull
私人字符串生日;
@NotNull
@ManyToOne(可选= false)
私有父亲1;
希望任何人都能告诉我如何实现 DELETE ON CASCADE
在我的系统中。
添加 mappedBy =parent1,cascade = CascadeType.ALL ,
父中的@OneToMany注释
Set< Child>子女
可以工作。
I'm implementing a simple user managment system with spring. To achieve better model structure I use inheritance. The system is build on three classes. The userdata
class which holds general information. The parent
class which inherits and extends the user class and finally a child
class which holds a parent object.
What I'm trying to do is to get all children (who are referred to a parent) deleted by deleting the parent
object.
I'm already able to save/delete objects to/from my mySQL-DB over my view, so the repositories/services/controllers seem to work.
To give you better understanding of my situation I put some code below.
My User
Class:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class UserData implements Persistable<String> {
private static final long serialVersionUID = 1L;
@Id
@NotNull
private String username;
@NotNull
private String password;
@NotNull
private String firstName;
@NotNull
private String lastName;
My Parent
Class which inherits from User and holds a set of Child:
@Entity
public class Parent extends UserData {
private static final long serialVersionUID = 1L;
private String imgName;
@OneToMany(fetch = FetchType.EAGER)
@ElementCollection
private Set<Child> children;
My Child
class which holds a Parent object
@Entity
@Transactional
public class Child implements Persistable<Long> {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
private String firstName;
@NotNull
private String lastName;
@NotNull
private String birthday;
@NotNull
@ManyToOne(optional = false)
private Parent parent1;
Hope anyone can tell me how I achieve DELETE ON CASCADE
in my system.
Adding mappedBy = "parent1", cascade = CascadeType.ALL, orphanRemoval = true
to the @OneToMany annotation in parent
by Set<Child> children
works.
这篇关于当使用继承和ManyToOne与JPA的关系时如何级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!