gpt4 book ai didi

hibernate - 无法删除或更新父行 : a foreign key constraint fails with Join tables

转载 作者:行者123 更新时间:2023-12-04 17:57:00 24 4
gpt4 key购买 nike

我正在使用 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, CONSTRAINT FK_SUBCATEGORY_CATEGORY FOREIGN KEY (CATEGORY_ID) REFERENCES category (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_categoryjob_subcategory),但不是 categorysubcategory 表。

感谢任何建议。

更新

我尝试删除 subcategoriescategories 然后是 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, CONSTRAINT fk_job_sub_subcategory FOREIGN KEY (SUBCAT_ID) REFERENCES subcategory (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, CONSTRAINT fk_person_job_person FOREIGN KEY (PER_ID) REFERENCES person (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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com