删除收藏与删除孤儿不工作与空分配? :( [英] remove collection with delete-orphan not work with null assignment? :(

查看:93
本文介绍了删除收藏与删除孤儿不工作与空分配? :(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过级联删除孤立删除另一个实体时遇到问题。
它在我清除关联的集合集合时起作用,但当我使集合集合为空时不起作用。
让我详细解释一下。
配置片段:

 < class name =com.sample.CategoriesDefaulttable =cats> ; 
< id name =idcolumn =idtype =stringlength =40access =property>
< generator class =assigned/>
< / id>
< version name =versioncolumn =objVrsunsaved-value =negative/>

< set name =blalazy =falsecascade =all-delete-orphaninverse =true>
< key column =idCatsnot-null =true/>
< / set>

< class name =com.sample.BLAtable =blaTEST>
< id name =idcolumn =idtype =stringlength =40access =property>
< generator class =assigned/>
< / id>
< version name =versioncolumn =objVrsunsaved-value =negative/>
< property name =blatype =stringcolumn =bla/>
< / class>

我的示例代码:

 分类cats = new CategoriesDefault(); 
final Set< BLA> col = new HashSet< BLA>();
col.add(新BLA(猫));
cats.setBla(col);
cats.saveOrupdate(); / /将更新/插入数据库。

以下工作正确,即:所有集合项都从db中移出。 p>

  cats.getBla()。clear(); 
cats.saveOrUpdate();

我认为这是可行的,因为调用此方法时,Hibernate的PersistSet标记为脏。



然而,下面的内容与上面的内容不一样,就像我想要的那样。

  cats.setBla(null); 
cats.saveOrUpdate();

如果我从数据库重新加载cats项目,它仍然包含BLA项目并且没有删除语句生成的Hibernate :( ..
为什么不???或者这是一个错误?我使用3.6.0.Final。

解决我认为它是因为hibernate使用它自己的集合实现(这就是为什么文档说你必须将集合声明为接口而不是实现),并且集合实现遵守传递持久性设置的语义。 ,当你做

  cats.getBla()。clear()

getBla()部分是hibernate集合实现,它知道在调用clear()时从会话中移除子元素。



当你做的时候

  cats.setBla(null); 

您没有删除集合,您已将父集对集合的引用更改为null。 n可能仍然存在于会话中。

I am having problems removing another entity through cascade delete-orphan. It works when I clear the associated set collection, but not when I make the set collection null. Let me explain in detail. The config snippet:

<class name="com.sample.CategoriesDefault" table="cats">
  <id name="id" column="id" type="string" length="40" access="property">
     <generator class="assigned" />
  </id>
  <version name="version" column="objVrs"  unsaved-value="negative"/>

 <set name="bla" lazy="false" cascade="all-delete-orphan" inverse="true">
      <key column="idCats" not-null="true"/>
       <one-to-many class="com.sample.BLA"/>
 </set>

<class name="com.sample.BLA" table="blaTEST">
   <id name="id" column="id" type="string" length="40" access="property">
     <generator class="assigned" />
   </id>
   <version name="version" column="objVrs"  unsaved-value="negative"/>
   <property name="bla" type="string" column="bla"/>
   <many-to-one name="parent" class="com.sample.CategoriesDefault" column="idCats" not-null="true"/>
</class>

My sample code:

Categories cats = new CategoriesDefault();
final Set<BLA> col = new HashSet<BLA>();
col.add(new BLA(cats));
cats.setBla(col);
cats.saveOrupdate(); // will update/insert it in the db.

The following works correctly, that is: all the collection items are all moved from the db.

cats.getBla().clear();
cats.saveOrUpdate();

I think this works as the PersistSet of Hibernate is marked as dirty when calling this method.

The following however doesn't work the same as above like I would want/expect.

cats.setBla(null);
cats.saveOrUpdate();

If I reload the cats item from the db, it still contains the BLA items and no delete statement is generated by Hibernate :(.. Why not??... or is this a bug ? I am using 3.6.0.Final.

解决方案

I think its because hibernate uses its own collection implementations (which is why the docs say you MUST declare collections as interfaces, not implementations), and the collection implementations obey the semantics of you transitive persistence settings. So, when you do

cats.getBla().clear()

the getBla() part is the hibernate collection implementation, which knows to remove the children from the session when clear() is called.

When you do

cats.setBla(null);

you haven't removed the collection, you have changed the parent's reference to the collection to null. The collection probably still exists in the session.

这篇关于删除收藏与删除孤儿不工作与空分配? :(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆