您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

即使一对多关系上的orphanRemoval = true,孤儿仍保留在数据库中(JPA /休眠)

即使一对多关系上的orphanRemoval = true,孤儿仍保留在数据库中(JPA /休眠)

我正在使用orphanRemoval=true单向一对多关联,没有任何问题。

实际上,我测试了您的代码和以下方案(正确AbstractPolicyRule实施equals/ hashCode):

Category category = new Category();
AbstractPolicyRule policyRule1 = new AbstractPolicyRule("foo");

category.addToActivePolicyRules(policyRule1);
em.persist(category);
em.flush();

assertNotNull(category.getId());
assertNotNull(category.getActivePolicyRules());
assertEquals(1, category.getActivePolicyRules().size());

category.removeFromActivePolicyRules(policyRule1);
category.addToActivePolicyRules(new AbstractPolicyRule("bar"));
// category = em.merge(category); // works with or without
em.flush();
assertEquals(1, category.getActivePolicyRules().size());

符合预期。在生成的跟踪下面:

22:54:30.817 [main]调试org.hibernate.sql-插入Category(id,category_name)值(空,?)
hibernate:插入类别(id,category_name)值(null,?)
22:54:30.824 [main] TRACE org.hibernate.type.StringType-将null绑定到参数:1
22:54:30.844 [main]调试ohid.IdentifierGeneratorHelper-本地生成的身份:1
...
22:54:30.872 [main]调试org.hibernate.sql-插入AbstractPolicyRule(id,name)值(空,?)
hibernate:插入AbstractPolicyRule(id,name)值(null,?)
22:54:30.873 [main] TRACE org.hibernate.type.StringType-将'foo'绑定到参数:1
22:54:30.874 [main]调试ohid.IdentifierGeneratorHelper-本机生成的身份:1
...
22:54:30.924 [main]调试org.hibernate.sql-更新AbstractPolicyRule设置category_policy_id =?id =?
hibernate:更新AbstractPolicyRule设置category_policy_id =?id =?
22:54:30.927 [main] TRACE org.hibernate.type.LongType-将“ 1”绑定到参数:1
22:54:30.928 [main] TRACE org.hibernate.type.LongType-将“ 1”绑定到参数:2
22:54:30.929 [main]调试ohpcAbstractCollectionPersister-完成插入收藏:插入1行
22:54:30.929 [main]调试org.hibernate.jdbc.AbstractBatcher-执行批处理大小:1
...
22:54:30.945 [main]调试org.hibernate.sql-插入AbstractPolicyRule(id,name)值(空,?)
hibernate:插入AbstractPolicyRule(id,name)值(null,?)
22:54:30.948 [main] TRACE org.hibernate.type.StringType-将“ bar”绑定到参数:1
22:54:30.948 [main]调试ohid.IdentifierGeneratorHelper-本机生成的身份:2
...
22:54:30.990 [main]调试org.hibernate.sql-更新AbstractPolicyRule设置category_policy_id = null,其中category_policy_id =?和id =?
hibernate:更新AbstractPolicyRule设置category_policy_id = null,其中category_policy_id =?和id =?
22:54:30.991 [main] TRACE org.hibernate.type.LongType-将“ 1”绑定到参数:1
22:54:30.992 [main] TRACE org.hibernate.type.LongType-将“ 1”绑定到参数2
22:54:30.993 [main]调试ohpcAbstractCollectionPersister-完成删除集合行:1已删除
22:54:30.993 [main]调试ohpcAbstractCollectionPersister-插入集合行:[com.stackoverflow.q3304092.Category.activePolicyRules#1]
22:54:30.994 [main]调试org.hibernate.jdbc.AbstractBatcher-执行批处理大小:1
...
22:54:30.996 [main]调试org.hibernate.sql-更新AbstractPolicyRule设置category_policy_id =?id =?
hibernate:更新AbstractPolicyRule设置category_policy_id =?id =?
22:54:30.997 [main] TRACE org.hibernate.type.LongType-将“ 1”绑定到参数:1
22:54:30.998 [main] TRACE org.hibernate.type.LongType-将“ 2”绑定到参数2
22:54:31.002 [main]调试ohpcAbstractCollectionPersister-完成插入行:已插入1
...
22:54:31.015 [main]调试org.hibernate.sql-从AbstractPolicyRule删除,其中id =?
hibernate:从AbstractPolicyRule中删除,其中id =?
22:54:31.017 [main] TRACE org.hibernate.type.LongType-将“ 1”绑定到参数:1

第一条策略规则将被删除

如果这不能代表您的工作,则可能应提供更多代码

回答来自OP的评论

哇,我刚刚将saveOrUpdate调用更改为merge,现在可以适当地删除它了。您对此有何见解?

只是一个猜测:既然orphanRemoval是JPA,我想知道是否saveOrUpdate可以适当地处理它(实际上,EntityManager自从您提到JPA以来,我以为您正在使用API)。

其他 2022/1/1 18:13:44 有750人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶