Hibernate 实体级联删除父级或子级
Hibernate entity cascade deletion parent or child
我有一些关于 "cascade" 的问题,
在我的项目中,我有类别 class 并且每个 class 都可以是父项或 child.But 我在相同的 class 中定义哪个父项或 child.There 是一个-父子之间的一对多关系。这是我的实体 class
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.*;
import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
@Getter
@Setter
@EqualsAndHashCode(exclude = {"recipes","categories","parentCategory","childrenCategory"})
@Entity
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String categoryName;
private String categoryDescription;
private String categoryUrl;
@OneToMany(mappedBy = "parentCategory",cascade = CascadeType.ALL)
private Set<Category> childrenCategory = new HashSet<>();
@ManyToOne
private Category parentCategory;
private boolean menuActive;
@ManyToMany(mappedBy = "categories")
Set<Recipe> recipes = new HashSet<>();
public Category addChildren(Category category){
this.getChildrenCategory().add(category);
category.setParentCategory(this);
return this;
}
}
我的问题是;
当我删除子类别时没问题,如果父类别有子类别,它 success.I 不能删除父类别。
错误信息;
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["FKBPI63NYEL12J6UG0D7S6BUAKX: PUBLIC.CAT_RECIPE FOREIGN KEY(CATEGORY_ID) REFERENCES PUBLIC.CATEGORY(ID) (5)"; SQL statement:
delete from category where id=? [23503-197]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
如何删除父类别?
解决方案
public void removeChildren(Category childCategory){
childCategory.setParentCategory(null);
}
你可以打电话给
category.get().getChildrenCategory().forEach(category.get()::removeChildren);
这是双向关系。所以你需要从双方删除关联。像这样:
public void removeChildren(Category childCategory){
this.getChildrenCategory().remove(childCategory);
childCategory.setParentCategory(null);
}
然后
parentCategory.getSubCategories().forEach(parentCategory::remove);
现在您可以删除 parentCategory
。
我有一些关于 "cascade" 的问题, 在我的项目中,我有类别 class 并且每个 class 都可以是父项或 child.But 我在相同的 class 中定义哪个父项或 child.There 是一个-父子之间的一对多关系。这是我的实体 class
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.*;
import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
@Getter
@Setter
@EqualsAndHashCode(exclude = {"recipes","categories","parentCategory","childrenCategory"})
@Entity
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String categoryName;
private String categoryDescription;
private String categoryUrl;
@OneToMany(mappedBy = "parentCategory",cascade = CascadeType.ALL)
private Set<Category> childrenCategory = new HashSet<>();
@ManyToOne
private Category parentCategory;
private boolean menuActive;
@ManyToMany(mappedBy = "categories")
Set<Recipe> recipes = new HashSet<>();
public Category addChildren(Category category){
this.getChildrenCategory().add(category);
category.setParentCategory(this);
return this;
}
}
我的问题是; 当我删除子类别时没问题,如果父类别有子类别,它 success.I 不能删除父类别。
错误信息;
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["FKBPI63NYEL12J6UG0D7S6BUAKX: PUBLIC.CAT_RECIPE FOREIGN KEY(CATEGORY_ID) REFERENCES PUBLIC.CATEGORY(ID) (5)"; SQL statement: delete from category where id=? [23503-197]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
如何删除父类别?
解决方案
public void removeChildren(Category childCategory){
childCategory.setParentCategory(null);
}
你可以打电话给
category.get().getChildrenCategory().forEach(category.get()::removeChildren);
这是双向关系。所以你需要从双方删除关联。像这样:
public void removeChildren(Category childCategory){
this.getChildrenCategory().remove(childCategory);
childCategory.setParentCategory(null);
}
然后
parentCategory.getSubCategories().forEach(parentCategory::remove);
现在您可以删除 parentCategory
。