JPA: violation of restrictions on deletion

I have three objects -

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 remove AppliationEntity, I get a constraint violation exception. I tried removing the application entry from CategoryEntity and then removing 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 much appreciated. Thanks at Advance.

+3
source share
2 answers

CategoryEntity refers to ApplicationEntity, so you have a constraint violation exception when you try to delete the specified ApplicationEntity instance.

You must include the field CascadeType.REMOVEin category ApplicationEntity.

EDIT:

The JPA documentation states that:

DELETE - If the owner of the object is deleted, the association target is also deleted.

, ApplicationEntity CategoryEntity . .

:

CascadeType.REMOVE buyers ApplicationEntity. ApplicationEntity UserEntity, ApplicationEntity, , ApplicationEntity , .

+3

, - / ApplicationEntity. CategoryEntity, OneToMany . ? .

ManyToMany UserEntity, , .

.

+2

All Articles