java JPA:删除时违反约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5936260/
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
JPA: constraints violation on delete
提问by Jahid
I have three entities -
我有三个实体 -
public class ApplicationEntity extends ModelEntity implements Application {
@ManyToOne(fetch = FetchType.LAZY, optional = false, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
private CategoryEntity category;
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinTable(
joinColumns = {@JoinColumn(name = "APPLICATION_ID")},
inverseJoinColumns = {@JoinColumn(name = "USER_ID")},
uniqueConstraints = {@UniqueConstraint(columnNames = {"APPLICATION_ID", "USER_ID"})
})
private List<UserEntity> buyers = new ArrayList<UserEntity>();}
public class CategoryEntity extends ModelEntity implements Category {
@Column(nullable = false)
private String name;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<ApplicationEntity> applications = new ArrayList<ApplicationEntity>();
}
public class UserEntity extends AbstractEntity implements User {
}
When I try to delete AppliationEntity, I get a constraints violation exception. I tried to remove application entry from CategoryEntity, and then delete the ApplicationEntity. But still fails. The exception is something like --
当我尝试删除 AppliationEntity 时,出现约束违规异常。我试图从 CategoryEntity 中删除应用程序条目,然后删除 ApplicationEntity。但还是失败了。例外是这样的——
Caused by: java.sql.SQLException: DELETE on table 'APPLICATIONENTITY' caused a violation of foreign key constraint 'FK109DF15D362F642' for key (32779). The statement has been rolled back.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 61 more
Caused by: ERROR 23503: DELETE on table 'APPLICATIONENTITY' caused a violation of foreign key constraint 'FK109DF15D362F642' for key (32779). The statement has been rolled back.
Any suggestion is highly appreciated. Thanks in Advance.
任何建议都受到高度赞赏。提前致谢。
回答by Gursel Koca
CategoryEntity has reference to ApplicationEntity, that is why you've got constraint violation exception, when you try to delete a referenced ApplicationEntity instance..
CategoryEntity 有对 ApplicationEntity 的引用,这就是为什么当您尝试删除引用的 ApplicationEntity 实例时会出现约束冲突异常的原因..
You should include CascadeType.REMOVE
to category
field of ApplicationEntity
.
你应该包括CascadeType.REMOVE
到category
的领域ApplicationEntity
。
EDIT :
编辑 :
JPA documentations says that :
JPA 文档说:
REMOVE – If the owning entity is removed, the target of the association is also removed.
REMOVE – 如果删除了拥有实体,则关联的目标也将被删除。
It means that when you delete ApplicationEntity , CategoryEntity wont be deleted. Only the associations between them would be deleted.
这意味着当您删除 ApplicationEntity 时, CategoryEntity 不会被删除。只会删除它们之间的关联。
EDIT :
编辑 :
You should add CascadeType.REMOVE
to buyers
field of ApplicationEntity
. There is relation table between ApplicationEntity and UserEntity, when you try to delete ApplicationEntity, all tuples that reference deleted ApplicationEntity in this relation table should be deleted before..
您应该添加CascadeType.REMOVE
到buyers
的领域ApplicationEntity
。ApplicationEntity 和 UserEntity 之间有关系表,当你尝试删除 ApplicationEntity 时,所有引用该关系表中被删除的 ApplicationEntity 的元组都应该先被删除。
回答by James
The error indicates that some other table/object has a reference to the ApplicationEntity. It does not seem to be CategoryEntity, as a OneToMany does not define a constraint. Are there other objects involved? You need to remove any references to an object before deleting it.
该错误表明某些其他表/对象引用了 ApplicationEntity。它似乎不是 CategoryEntity,因为 OneToMany 没有定义约束。是否涉及其他对象?在删除对象之前,您需要删除对对象的所有引用。
It could be the constraint from the ManyToMany join table to UserEntity, but the remove should be deleting from the join table first, so that is odd.
它可能是从 ManyToMany 连接表到 UserEntity 的约束,但删除应该首先从连接表中删除,所以这很奇怪。
Can you include the full exception stack trace.
您能否包含完整的异常堆栈跟踪。