简体   繁体   English

JPA:删除约束违规

[英]JPA: constraints violation on delete

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. 当我尝试删除AppliationEntity时,我得到一个约束违规异常。 I tried to remove application entry from CategoryEntity, and then delete the ApplicationEntity. 我尝试从CategoryEntity中删除应用程序条目,然后删除ApplicationEntity。 But still fails. 但仍然失败。 The exception is something like -- 例外是这样的 -

    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. 提前致谢。

CategoryEntity has reference to ApplicationEntity, that is why you've got constraint violation exception, when you try to delete a referenced ApplicationEntity instance.. 当您尝试删除引用的ApplicationEntity实例时,CategoryEntity引用了ApplicationEntity,这就是您遇到约束违例异常的原因。

You should include CascadeType.REMOVE to category field of ApplicationEntity . 您应该将CascadeType.REMOVE包含在ApplicationEntity category字段中。

EDIT : 编辑:

JPA documentations says that : JPA文件说:

REMOVE – If the owning entity is removed, the target of the association is also removed. 删除 - 如果删除了拥有实体,则也会删除关联的目标。

It means that when you delete ApplicationEntity , CategoryEntity wont be deleted. 这意味着当您删除ApplicationEntity时,不会删除CategoryEntity。 Only the associations between them would be deleted. 只删除它们之间的关联。

EDIT : 编辑:

You should add CascadeType.REMOVE to buyers field of ApplicationEntity . 您应该将CascadeType.REMOVE添加到ApplicationEntity buyers字段。 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.. ApplicationEntity和UserEntity之间存在关系表,当您尝试删除ApplicationEntity时,应该删除在此关系表中引用已删除的ApplicationEntity的所有元组。

The error indicates that some other table/object has a reference to the ApplicationEntity. 该错误表示某些其他表/对象具有对ApplicationEntity的引用。 It does not seem to be CategoryEntity, as a OneToMany does not define a constraint. 它似乎不是CategoryEntity,因为OneToMany没有定义约束。 Are there other objects involved? 是否涉及其他对象? You need to remove any references to an object before deleting it. 在删除对象之前,您需要删除对该对象的任何引用。

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. 可以包含完整的异常堆栈跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM