我有三个表 Employee、Boss 和 Address。
在这种情况下,员工和老板共享相同的地址。当我在 Employee 上调用 EntityManager.remove 时,我得到一个 Integrity Constraint 异常,因为它试图删除它不能删除的地址,因为老板仍然需要它。如果没有人使用该地址,但我希望将其删除。我的注释应该是什么样子,以便我可以从 Address 中删除孤儿但避免完整性约束?
例外 =
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: DELETE on
table 'Employee' caused a violation of foreign key constraint 'Boss....
代码 =
public class Employee {
@OneToMany(targetEntity = Address.class, orphanRemoval = true,cascade = {
CascadeType.ALL
} fetch=FetchType.EAGER)
@JoinTable(name = "Employee_Address")
@XmlElement(required = true)
@OrderColumn
protected List<Address> addresses;
}
public class Boss {
@OneToMany(targetEntity = Address.class, orphanRemoval = true, cascade = {
CascadeType.ALL
}fetch=FetchType.EAGER)
@JoinTable(name = "Boss_Address")
@XmlElement(required = true)
@OrderColumn
protected List<Address> addresses;
}
地址类对老板或员工一无所知。