- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有三个表:员工、老板和地址。
在本例中,员工和老板共享相同的地址。当我对 Employee 调用 EntityManager.remove 时,我收到完整性约束异常,因为它尝试删除地址,但它不能删除,因为老板仍然需要它。如果没有人使用该地址,我希望将其删除。我的注释应该是什么样子,以便我可以从 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;
}
地址类对老板或员工一无所知。
最佳答案
您的注释是正确的。删除员工时,它将尝试删除其地址。
但如果两者使用相同的地址,删除将会失败。
考虑到这种情况,请使用不带 CasacadeType.DELETE 或 CasacadeType.ALL 的级联选项列表,并以编程方式解决问题。
也不要在此使用 orphanRemoval 。请参阅 JPA 2.0 规范,第 2.9 节:
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.
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).
关于java - JPA级联都导致完整性约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17708687/
我想要一些概念上的澄清。为了证明问题是 NP 完全的,我们使用归约。 现在假设我有 L<=L'。是从 L 减少到 L' 还是我也可以用相反的方式来减少?即我能否证明如果 L 可以使用 L' 求解,那么
考虑不相交的哈密顿路径问题: 输入:一个可能是有向或无向的图 输出:此图是否至少存在 2 条边不相交的哈密顿路径?边不相交意味着没有一条边被两条路径共享。 证明不相交哈密顿路径是 np-完全的。 有人
我理解为什么有界度生成树被认为是度数为 2 的 NP 完全(这是哈密顿路径问题的一个实例),但我不明白为什么这适用于度数 > 2。如果有人可以解释一下为什么这是大于 2 的 NP 完全问题,这将是最有
我正在实现一个 Django 网站,其中上传的文件在保存到服务器 (/media) 之前使用用户提供的 key 进行加密。当用户希望查看它们时,系统会提示他们输入 key ,加密文件被解密,然后显示给
我想用nodejs列出指定目录中的所有文件。 var fs = require('fs'); var path = require('path'); var walk = function(direc
在我的文件夹 assets/data 中,有很多包含我的应用静态数据的 XML 文件。 对于某人来说,检索 APK、修改其中的一部分并安装到设备上真的很容易。 我想通过检查我的 assets/data
我正在努力将我的备份脚本从 shell 转换为 Python。我的旧脚本的功能之一是通过执行以下操作检查创建的 tarfile 的完整性:gzip -t。 这在 Python 中似乎有点棘手。 似乎唯
我正在尝试将包含带有单独 CSS 和 js 文件的 HTML 脚本的 php 文件导入另一个包含我的页眉和页脚的 php 文件。页眉和页脚来自一个模板,该模板使用非常困惑和令人费解的 CSS,基本上对
使用 Flask,我试图验证 cookie 没有被篡改。现在,如果我更改 cookie 值,它只会抛出一个错误,但我想检查代码 is_valid(session['user_id']) 并重定向/重置
在 PHP(和 MySQL)中,我们有许多技术来确保输入的数据有效且安全。添加斜杠、MySQL 的转义字符串和正则表达式是我们经常使用的一些。 我已经看到此链接,该链接对该主题进行了非常初步的介绍,但
下面的代码使用了不安全的 GeneralizedNewtypeDeriving扩展中断 Data.Set通过插入具有不同 Ord 的不同元素实例: {-# LANGUAGE GeneralizedNe
我刚刚在 NPM 上创建了一个新包(这非常简单),我对如何维护包的完整性感兴趣。任何人都可以发布软件包的新版本吗?或者这仅限于我的用户帐户? 如果任何人都可以发布对包的更改,如何监控他们的修改以确保项
我正在尝试使用 Dapper 和 SQLite 来追踪 C# 项目中的数据库损坏错误。所以我正在寻找一种方法来检查代码中的数据库完整性。我发现多个地方说我可以为此发送命令“PRAGMAintegrit
yarn 安装抛出: EACCES: permission denied, unlink '/home/minnak/Darbas/market/node_modules/.yarn-integrit
上下文: 我有 open-sourced a repository ,由 Travis-CI 测试。特拉维斯提供 build-notification用于测试运行的钩子(Hook),因此您可以在 IR
我是一名优秀的程序员,十分优秀!