我确切地说这个表(省略字段无关紧要):
ID | NAME | PARENT_ID
添加两行后,它看起来很精确:
ID | NAME | PARENT_ID
1 "ONE" null
2 "TWO" 1
现在,当我删除第二行时,第一行也会被删除。我想只从父级到子级进行级联,所以当我删除ID为2的行时,它应该只删除这一行。如果我删除ID为1的行,则应删除这两行。有人可以帮助我实现这个目标吗?
这就是我的实体的样子:
@Entity
@Getter
@DynamicUpdate
@EqualsAndHashCode(of = "name")
@NoArgsConstructor
@EntityListeners({AuditingEntityListener.class})
class Category {
@Id
@GeneratedValue(generator = "ID_GENERATOR")
private Long id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "category", cascade = CascadeType.ALL)
private Set<Product> products;
@Setter
@Accessors(chain = true)
@Column(unique = true, length = 20, nullable = false)
private String name;
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "parent_category_id")
@Setter
@Accessors(chain = true)
private Category parentCategory;
@OneToMany(mappedBy = "parentCategory")
private Set<Category> subCategories = new HashSet<>();
@CreatedDate
@Column(updatable = false)
@Getter
private Instant creationDate;
@LastModifiedDate
@Getter
@Version
private Instant lastModifiedDate;
Category(String name, Category parentCategory){
this.name = name;
this.parentCategory = parentCategory;
}
}