@ManyToMany 在删除时违反参照完整性约束
我有这种多对多关系
@ManyToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER)
@JoinTable(
name="tbl_settings_employees",
joinColumns = @JoinColumn(name = "id"),
inverseJoinColumns = @JoinColumn( name = "employee_id")
)
private Set<Employee> employeeSet;
这不允许我删除主要对象。换句话说,就我而言,我无法从数据库中删除员工。
对于这个示例,我有 4 个表,一个用户表、一个设置表、一个 settings_edit_employee_set 表和一个员工表。
这些是表格和关系:
|---------| |----------------------------|
| tbl_user| | tbl_settings |
| --------| | ---------------------------|
| Id (PK) | | Id (PK)| user_id (PK ref tbl_user)
|---------| |----------------------------|
| 1 | | 1 | 1 |
| 2 | | 2 | 2 |
| 3 | | 1 | 2 |
| 4 | | 2 | 1 |
| etc | |etc |etc |
|---------| |----------------------------|
|-------------------------------|
| tbl_settings_employees (pk=composite)
| ------------------------------|
| Id (PK ref tbl_settings) |
| | Employee_ID(PK ref tbl_employee)
|-------------------------------|
| 1 | 150 |
| 1 | 149 |
| 2 | 150 |
| 2 | 151 |
| etc | etc |
|-------------------------------|
|-------------|
| tbl_employee|
| ------------|
| Id PK |
|-------------|
| 149 |
| 150 |
| 151 |
| etc |
|-------------|
简而言之:
我想从我的数据库中删除一个对象,但我现在不能;因为它给出了参照完整性约束违反异常:
org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation:
"FKE5EF0384FB556B15: PUBLIC.TBL_SETTINGS FOREIGN KEY(EMPLOYEE_EMPLOYEE_ID) REFERENCES
PUBLIC.TBL_EMPLOYEE(EMPLOYEE_ID) (2)"; SQL statement:
delete from tbl_employee where bsn='234234234' [23503-166]