@ManyToMany参照完整性约束违反ON DELETION

时间:2012-06-21 15:40:34

标签: hql

@ManyToMany参考完整性约束违反ON DELETION


我得到了很多关系

  @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]

2 个答案:

答案 0 :(得分:0)

尝试:cascade = CascadeType.DETACH

答案 1 :(得分:0)

有类似的问题

@ManyToMany(targetEntity = DdoMaster.class)
    @Cascade(value = {CascadeType.DELETE, CascadeType.SAVE_UPDATE})
    @JoinTable(name = "acct_account_ddo_mapping", joinColumns = @JoinColumn(name = "fk_accountid", referencedColumnName = "pk_accountname"), inverseJoinColumns = @JoinColumn(name = "fk_ddoid", referencedColumnName = "pk_ddoid"))
    @RemoteProperty
    public Set<DdoMaster> getDdoMaster() {
        return ddoMaster;
    }

    public void setDdoMaster(Set<DdoMaster> ddoMaster) {
        this.ddoMaster = ddoMaster;
    }

现在从表中删除行时它会抛出Dataintegrity异常,只需先清除该集合并稍后删除该行....