简体   繁体   中英

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. I tried to remove application entry from CategoryEntity, and then delete the 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..

You should include CascadeType.REMOVE to category field of ApplicationEntity .

EDIT :

JPA documentations says that :

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. Only the associations between them would be deleted.

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..

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.

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.

Can you include the full exception stack trace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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