JPA级联都会导致完整性约束 [英] JPA cascade all causing integrity constraint

查看:87
本文介绍了JPA级联都会导致完整性约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表Employee,Boss和Address.

I have three tables Employee, Boss, and Address.

在这种情况下,雇员和老板共享相同的地址.当我在Employee上调用EntityManager.remove时,我收到一个Integrity Constraint异常,因为它试图删除由于Boss仍需要它而无法删除的地址.如果没有人使用该地址,尽管我希望将其删除.我的注释应该是什么样子,以便我可以从Address删除孤立对象,但避免完整性约束?

Employee and Boss in this case share the same Address. When I call EntityManager.remove on Employee I get an Integrity Constraint exception because it tries to delete the Address which it can't because Boss still needs it. If no one is using the Address though I would want it to be removed. What should my annotations look like so that I can delete orphans from Address but avoid the integrity constraint?

Exception =

Exception =

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;
}

地址类对老板或雇员一无所知.

Address class knows nothing about boss or employee.

推荐答案

您的注释正确.删除Employee时,它将尝试删除其地址.

Your annotations are correct. When deleting Employee it will attempt to remove it's Addresses.

但是如果两个都使用相同的地址,则删除将失败.

But if both uses same Address the removal will fail.

鉴于这种情况,请使用不带CasacadeType.DELETE或CasacadeType.ALL的级联选项列表,并以编程方式解决该问题.

Given this scenario use a list of cascades options without CasacadeType.DELETE or CasacadeType.ALL and solve the issue programatically.

也不要在此使用orphanRemoval.请参阅JPA 2.0规范的第2.9节:

Also do not use orphanRemoval in this. See the JPA 2.0 spec, section 2.9:

如果从关系中移除了作为关系目标的实体(通过设置 关系为null或从关系集合中移除实体),则移除操作将应用于孤立的实体.删除操作适用于 冲洗操作. orphanRemoval功能适用于其父实体私有"拥有的实体. 便携式应用程序必须不依赖于 特定的移除顺序,并且不得将已孤立的实体重新分配给另一个 关系或尝试保持关系.如果孤立的实体是独立的新实体, 或已删除的实体,则orphanRemoval的语义不适用.

If an entity that is the target of the relationship is removed from the relationship (by setting the relationship to null or removing the entity from the relationship collection), the remove operation will be applied to the entity being orphaned. The remove operation is applied at the time of the flush operation. The orphanRemoval functionality is intended for entities that are privately "owned" by their parent entity. Portable applications must otherwise not depend upon a specific order of removal, and must not reassign an entity that has been orphaned to another relationship or otherwise attempt to persist it. If the entity being orphaned is a detached, new, or removed entity, the semantics of orphanRemoval do not apply.

如果将删除操作应用于托管源实体,则删除操作将为 根据3.2.3节的规则级联到关系目标,(因此 不必为该关系指定cascade = REMOVE).

If the remove operation is applied to a managed source entity, the remove operation will be cascaded to the relationship target in accordance with the rules of section 3.2.3, (and hence it is not necessary to specify cascade=REMOVE for the relationship).

这篇关于JPA级联都会导致完整性约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆