gpt4 book ai didi

hibernate - 使用 Hibernate 删除一对多关系中的对象时出现 PostgreSQL 异常

转载 作者:行者123 更新时间:2023-11-29 13:05:38 25 4
gpt4 key购买 nike

我有非常简单的架构 - 一对多关系中的两个表:

<hibernate-mapping package="my.app">
<class name="CorrelationKey" table="CORRELATION_KEYS">
<id name="id" column="CORRELATION_ID">
<generator class="native"/>
</id>
<set name="correlationKeyParts" cascade="all-delete-orphan" lazy="false">
<key column="CORRELATION_ID"/>
<one-to-many class="CorrelationKeyPart"/>
</set>
...
</class>
</hibernate-mapping>

CorrelationKey 类型的对象的保存和更新工作正常,并且更改传播到集合。但是,当我尝试删除 CorrelationKey 对象时,出现以下错误:

108090 [main] ERROR org.hibernate.util.JDBCExceptionReporter  - ERROR: update or 
delete on table "correlation_keys" violates foreign key constraint "fk23630e23e9b29c52"
on table "correlation_key_parts"
Detail: Key (correlation_id)=(162) is still referenced from table "correlation _key_parts".

我在 Windows 机器上使用 PostgreSQL 9.1。

非常感谢。

最佳答案

当您执行这样的 DML 查询时, session 缓存、级联和版本检查将被完全绕过。查询直接转换为 SQL 查询并按原样执行。因此,您有责任首先删除(或修改)依赖实体。如果要应用级联,需要使用Hibernate API:

List<CorrelationKey> allKeys = session.createQuery("select k from CorrelationKey k").list();
for (CorrelationKey key : allKeys) {
session.delete(key);
}

关于hibernate - 使用 Hibernate 删除一对多关系中的对象时出现 PostgreSQL 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13536092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com