java JPA:删除的外键约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15917693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:16:52  来源:igfitidea点击:

JPA: Foreign key constraint on delete

javamysqlhibernatejpa

提问by shazinltc

I have the following relationship

我有以下关系

Class UserAccount{
//Other fields
@OneToMany(mappedBy = "userAccount", cascade = CascadeType.REMOVE)
private Set<Images> imagesShared;

@ManyToMany
@JoinTable(name = "USER_LIKES", joinColumns = @JoinColumn(name = "USER_NAME"),   inverseJoinColumns = @JoinColumn(name = "ID"))
private Set<Images> imagesLiked;
}

Class Images{
//other fields
@ManyToMany(mappedBy = "imagesLiked")
private Set<UserAccount> likes;
}

I get an exception after these lines

在这些行之后我得到一个例外

Hibernate: delete from IMAGES where ID=?
Hibernate: delete from COMMENTS where COMMENT_ID=?
Hibernate: delete from COMMENTS where COMMENT_ID=?
Hibernate: delete from COMMENTS where COMMENT_ID=?
Hibernate: delete from COMMENTS where COMMENT_ID=?
Hibernate: delete from COMMENTS where COMMENT_ID=?
Hibernate: delete from IMAGES where ID=?

Exception:

例外:

Cannot delete or update a parent row: a foreign key constraint fails (`testdb`.`USER_LIKES`, CONSTRAINT `FKC6704E28B4E3D8B` FOREIGN KEY (`ID`) REFERENCES `IMAGES` (`ID`))

From my understanding this happens when JPA tries to remove imagesShared. I tried doing this:

据我了解,当 JPA 尝试删除imagesShared. 我试过这样做:

for (Images image : userAccount.getImagesShared()){
            image.setLikes(null);
        }
em.remove(account);

But same error. Anyone?

但同样的错误。任何人?

UPDATE

更新

When I add this line it works fine.

当我添加这一行时,它工作正常。

 for (Images image : userAccount.getImagesShared()){
            image.setLikes(null);
        }
userAccount.getImagesShared().clear();
em.remove(account);

But what is the difference between the remove operation that JPA does and what I do?

但是JPA做的remove操作和我做的有什么区别呢?

回答by Thihara

One of your private Set<Images> imagesSharedor private Set<Images> imagesLikedmust have a reference to the Image you are trying to delete.

您的一个private Set<Images> imagesSharedprivate Set<Images> imagesLiked必须引用您要删除的图像。

You will have to loop through everything and remove the image reference before removing it.

在删除之前,您必须遍历所有内容并删除图像参考。

EDIT :

编辑 :

To answer your second question.

回答你的第二个问题。

 for (Images image : userAccount.getImagesShared()){
            image.setLikes(null);
        }
userAccount.getImagesShared().clear();
em.remove(account);

works because you are clearing the image references stored in userAccountby calling userAccount.getImagesShared().clear()

有效,因为您正在userAccount通过调用清除存储的图像引用userAccount.getImagesShared().clear()

Previously what you were doing was simply removing the likes stored in that Image, not removing the images from the userAccountobject itself.

以前您所做的只是删除存储在该图像中的喜欢,而不是从userAccount对象本身中删除图像。

回答by Lakshmi

The private Set<Images> imagesLikedhas a foreign key reference to the IMAGES(ID) so if you are trying to delete the private Set<Images> imagesShared;without deleting the private Set<Images> imagesLiked;.

Theprivate Set<Images> imagesLiked具有对IMAGES( ID)的外键引用, 因此如果您尝试删除private Set<Images> imagesShared;而不删除private Set<Images> imagesLiked;.

So try removing the imagesLinked also

所以也尝试删除 imagesLinked

image.setImagesLinked(null);