- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Java 8 和 Hibernate5/JPA2。
当我尝试删除 Job
对象时出现以下错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (
www
.subcategory
, CONSTRAINTFK_SUBCATEGORY_CATEGORY
FOREIGN KEY (CATEGORY_ID
) REFERENCEScategory
(ID
) ON DELETE NO ACTION ON UPDATE NO ACTION)
表格
+============+ +=================+ +================+
| job | | job_category | | category |
+============+ +=================+ +================+
| ID | | JOB_ID | | ID |
| | | CAT_ID | | |
+============+ +=================+ +================+
+==================+ +=================+
| job_subcategory | | subcategory |
+==================+ +=================+
| JOB_ID | | ID |
| SUBCAT_ID | | CATEGORY_ID |
+==================+ +=================+
一个Job
可以有很多Categories
。一个 Category
可以有很多 SubCategories
。
Job.java
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "job_category", joinColumns = {
@JoinColumn(name = "JOB_ID", referencedColumnName = "ID") }, inverseJoinColumns = {
@JoinColumn(name = "CAT_ID", referencedColumnName = "ID") })
private Set<Category> categories;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "job_subcategory", joinColumns = {
@JoinColumn(name = "JOB_ID", referencedColumnName = "ID") }, inverseJoinColumns = {
@JoinColumn(name = "SUBCAT_ID", referencedColumnName = "ID") })
private Set<SubCategory> subCategories;
类别.java
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER, mappedBy = "category")
private Set<SubCategory> subCategories;
子类别.java
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "CATEGORY_ID")
private Category category;
JpaDao.java
protected void remove(Job entity) {
entityManager.remove(entity);
}
当我删除 Job
对象时,我希望它从 job
表和连接表(job_category
和job_subcategory
),但不是 category
和 subcategory
表。
感谢任何建议。
更新
我尝试删除 subcategories
和 categories
然后是 job
:
public boolean delete(Job job) {
Set<SubCategory> subCategories = job.getSubCategories();
if (subCategories != null) {
for (SubCategory subCategory : subCategories) {
entityManager.remove(subCategory);
}
}
Set<Category> categories = job.getCategories();
if (categories != null) {
for (Category category : categories) {
entityManager.remove(category);
}
}
entityManager.remove(job);
return true;
}
但我仍然得到同样的错误:
Cannot delete or update a parent row: a foreign key constraint fails (
www
.job_subcategory
, CONSTRAINTfk_job_sub_subcategory
FOREIGN KEY (SUBCAT_ID
) REFERENCESsubcategory
(ID
) ON DELETE NO ACTION ON UPDATE NO ACTION)
该错误仅在我完成事务 (@Transactional
) 时发生,所以它看起来像是在 Hibernate 执行提交时发生的。
更新
我尝试以下操作:
public boolean delete(Job job) {
job.setSubCategories(null);
job.setCategories(null);
super.remove(job);
return true;
}
但再次得到以下内容:
Cannot delete or update a parent row: a foreign key constraint fails (
www
.person_job
, CONSTRAINTfk_person_job_person
FOREIGN KEY (PER_ID
) REFERENCESperson
(ID
) ON DELETE NO ACTION ON UPDATE NO ACTION)
更新
我尝试:
public boolean delete(Job job) {
job.setSubCategories(null);
job.setCategories(null);
job.setPerson(null);
super.remove(job);
return true;
}
并得到:
IllegalArgumentException: Source must not be null
最佳答案
好吧,试试这样的东西
public boolean delete(Job job) {
Set<SubCategory> subCategories = job.getSubCategories();
if (subCategories != null) {
job.getSubCategories().clear();
for (SubCategory subCategory : subCategories) {
subCategory.setJob(null);
entityManager.remove(subCategory);
}
}
Set<Category> categories = job.getCategories();
if (categories != null) {
job.getCategories().clear();
for (Category category : categories) {
category.setJob(null);
entityManager.remove(category);
}
}
job.setPerson(null);
//if necessary:
//Person.setJob(null);
entityManager.remove(job);
return true;
}
此外,为什么您有 super.remove(job)
而不是我刚刚发布的代码中的 entityManager.remove(job)
?
这段代码是否仍然出现异常?
关于hibernate - 无法删除或更新父行 : a foreign key constraint fails with Join tables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39700446/
我是一名优秀的程序员,十分优秀!