Spring开机;当我删除数据时,它会删除连接到该外键的所有数据
Spring boot; When I delete the data, it deletes all the data connected to that foreign key
我有一个 question.When 我用 jpa 从类别 table 中删除了所有数据;它会删除它在位置 table 中连接到的父级以及该父级连接到的所有类别。
样本;
My Categories Data
如果我删除类别 table 中的 ID 113;它会删除类别 table 中的所有 location_id 1 和位置 table 中 ID 为 1 的位置。结果,location_id 为 1 的任何内容都将被删除。
My Tables
我的位置:
@Getter
@Setter
@Entity
@Table(name = "location_entity")
public class LocationEntity extends BaseEntity {
// some properties
@Fetch(FetchMode.SUBSELECT)
@OneToMany(mappedBy = "categorylocation")
private List<CategoryEntity> categoryEntityList;
// some properties
}
我的分类:
@Getter
@Setter
@Entity
@Table(name = "category_entity")
public class CategoryEntity extends BaseEntity {
// some properties
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "location_id")
private LocationEntity categorylocation;
// some properties
}
此行为与 ManyToOne 注释的 cascade 参数有关,
从 categorylocation 属性中删除 cascade = CascadeType.ALL
将解决问题。
我有一个 question.When 我用 jpa 从类别 table 中删除了所有数据;它会删除它在位置 table 中连接到的父级以及该父级连接到的所有类别。
样本;
My Categories Data
如果我删除类别 table 中的 ID 113;它会删除类别 table 中的所有 location_id 1 和位置 table 中 ID 为 1 的位置。结果,location_id 为 1 的任何内容都将被删除。
My Tables
我的位置:
@Getter
@Setter
@Entity
@Table(name = "location_entity")
public class LocationEntity extends BaseEntity {
// some properties
@Fetch(FetchMode.SUBSELECT)
@OneToMany(mappedBy = "categorylocation")
private List<CategoryEntity> categoryEntityList;
// some properties
}
我的分类:
@Getter
@Setter
@Entity
@Table(name = "category_entity")
public class CategoryEntity extends BaseEntity {
// some properties
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "location_id")
private LocationEntity categorylocation;
// some properties
}
此行为与 ManyToOne 注释的 cascade 参数有关,
从 categorylocation 属性中删除 cascade = CascadeType.ALL
将解决问题。