Java 休眠自加入-防止从子级级联中删除
我有这个表(省略的字段无关紧要): 添加两行后,看起来非常精确:Java 休眠自加入-防止从子级级联中删除,java,hibernate,Java,Hibernate,我有这个表(省略的字段无关紧要): 添加两行后,看起来非常精确: ID | NAME | PARENT_ID 1 "ONE" null 2 "TWO" 1 现在,当我删除第二行时,第一行也被删除了。我只想从父级级联到子级,所以当我删除ID为2的行时,它应该只删除这一行。如果我删除ID为1的行,它应该同时删除这两行。有人能帮我做到这一点吗 这就是我的实体的外观: @Entity @Getter @DynamicUpdate @Equals
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;
}
}
@实体
@吸气剂
@动态铜日期
@EqualsAndHashCode(of=“name”)
@诺尔格构装师
@EntityListeners({AuditingEntityListener.class})
类别{
@身份证
@GeneratedValue(generator=“ID_generator”)
私人长id;
@OneToMany(fetch=FetchType.LAZY,mappedBy=“category”,cascade=CascadeType.ALL)
私人套装产品;
@塞特
@访问器(链=真)
@列(unique=true,length=20,nullable=false)
私有字符串名称;
@manytone(cascade={CascadeType.ALL})
@JoinColumn(name=“父类\类别\ id”)
@塞特
@访问器(链=真)
私人类;
@OneToMany(mappedBy=“parentCategory”)
私有集子类别=新HashSet();
@创建数据
@列(可更新=false)
@吸气剂
私人即时创作;
@最后修改日期
@吸气剂
@版本
私人即时通讯;
类别(字符串名称、类别父类别){
this.name=名称;
this.parentCategory=parentCategory;
}
}
我只想从父级级联到子级:那么为什么要在parentCategory上指定@ManyToOne(cascade={CascadeType.ALL})
。这正是你不想做的。既然这就是你想要的,为什么不在OneToMany上指定一个级联呢?对,我的错。谢谢:)
@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;
}
}