删除实体时违反java完整性约束
我有四个实体客户,ClientAccount,ClientProductAccount和ClientProduct。客户端包含一组ClientAccount,ClientAccount包含一组ClientProductAccount,ClientProductAccount引用ClientProduct。客户还拥有一套客户产品。我正在做的是删除ClientProduct,所以我需要做的是删除给定ClientProduct的ClientProductAccount,然后从客户端删除ClientProduct。 经过这个过程,我得到了
org.hsqldb.HsqlException: integrity constraint violation: foreign key no action; FK_7DMBVW6DMUIW2SFWLAQPS42HQ table: CLIENTPRODUCTACCOUNT
到目前为止,我检查了仅删除ClientProductAccount时的行为,它删除了该实体,结果是:
Hibernate:
delete
from
ClientProductAccount
where
id=?
and version=?
另一方面,当我添加任务的下一部分从客户机实体中删除ClientProduct时,我得到了完整性约束冲突和以下sql命令的异常:
Hibernate:
update
ClientProductAccount
set
version=?,
accountPriority=?
where
id=?
and version=?
Hibernate:
delete
from
ClientProduct
where
id=?
and version=?
型号:
@Entity
public class Client {
...
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "client_id", nullable = false, updatable = false)
private Set<ClientAccount> accounts = new HashSet<>();
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "client_id", nullable = false, updatable = false)
private Set<ClientProduct> products = new HashSet<>();
...
}
@Entity
public class ClientAccount {
...
@OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, orphanRemoval = true)
@JoinColumn(name = "clientAccount_id", nullable = false, updatable = false)
Set<ClientProductAccount> productOfAccountSet = new HashSet<>();
...
}
@Entity
public class ClientProductAccount {
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "clientProduct_id", nullable = false, updatable = false)
ClientProduct product;
...
}
和客户产品:
@Entity
public class ClientProduct {
@NotNull
@Enumerated(EnumType.STRING)
@Column(nullable = false, length = CommonColumns.ENUM_LENGTH)
ProductType productType;
...
}
负责删除的代码:
Set<ProductType> toBeDeletedProductsSet = client.getProducts().stream()
.map(ClientProduct::getProductType)
.filter(productType -> !productTypeList.contains(productType))
.collect(toSet());
client.getAccounts().forEach(
clientAccount -> clientAccount.getProductOfAccountSet().removeIf(
clientProductAccount -> toBeDeletedProductsSet.contains(clientProductAccount.getProduct().getProductType())
)
);
client.getProducts()
.removeIf(clientProduct -> !productTypeList.contains(clientProduct.getProductType()));
结果应该是,给定ClientProduct的ClientProductAccount应该被删除,来自客户端实体的ClientProduct也应该被删除。但我有以下例外:
Caused by: org.hsqldb.HsqlException: integrity constraint violation: foreign key no action; FK_7DMBVW6DMUIW2SFWLAQPS42HQ table: CLIENTPRODUCTACCOUNT at org.hsqldb.error.Error.error(Unknown Source) at org.hsqldb.StatementDML.performReferentialActions(Unknown Source) at org.hsqldb.StatementDML.delete(Unknown Source) at org.hsqldb.StatementDML.executeDeleteStatement(Unknown Source) at org.hsqldb.StatementDML.getResult(Unknown Source) at org.hsqldb.StatementDMQL.execute(Unknown Source) at org.hsqldb.Session.executeCompiledStatement(Unknown Source) at org.hsqldb.Session.execute(Unknown Source) ... 63 more
共 (0) 个答案