J'ai la relation suivante
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;
}
J'obtiens une exception après ces lignes
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`))
D'après ce que j'ai compris, cela se produit lorsque JPA essaie de supprimer imagesShared
. J'ai essayé de faire ça :
for (Images image : userAccount.getImagesShared()){
image.setLikes(null);
}
em.remove(account);
Mais même erreur. Quelqu'un ?
UPDATE
Lorsque j'ajoute cette ligne, tout fonctionne bien.
for (Images image : userAccount.getImagesShared()){
image.setLikes(null);
}
userAccount.getImagesShared().clear();
em.remove(account);
Mais quelle est la différence entre l'opération de suppression que JPA effectue et ce que je fais ?