Hibernate不插入重新附加的集合? (bug?)

版本:3.6.0.final 我有以下情况: 具有集合的实体,包含值类型对象,完全启用了级联。 1)我删除,收集并将其存储到数据库。然后加载它以检查集合是否确实被删除。 2)然后我添加删除的集合并再次存储实体。然后我再次加载它以检查结果并注意到该集合是空的,它不应该:( ... 我做错了什么或这是一个错误? 当我在2)中使用干净创建的集合时,Hibernate对象不存在,它工作正常..,即:集合正确存储在数据库中。 在代码中,我的映射:
<class name="com.sample.Declaration" table="decl">
<id name="id" column="id" type="string" length="40" access="property">
   <generator class="assigned" />
</id>
<set name="statusHistory" table="decl_sts_hist" lazy="false" cascade="all">
   <cache usage="read-write" />
   <key column="idDec" not-null="true" />
   <composite-element class="com.sample.DeclarationStatusDefault">
     <property name="remark" column="remark" type="string" length="254" />
     <property name="statusName" column="stsName" type="declarationStatusName" not-null="true" update="false" />
   </composite-element>
</set>
</class>
代码:
// 1): clear the status history
Declaration dec = Persister.findDeclarationById("id");
SortedSet<DeclarationStatus> history = dec.getStatusHistory();
dec.setStatusHistory(null);
dec.saveOrUpdate(); // will call saveOrUpdate on the current session.

Declaration dec = Persister.findDeclarationById("id");
assertNull(dec.getStatusHistory()); // OK

// 2) recover the status history
dec.setStatusHistory(history);
dec.saveOrUpdate(); // will call saveOrUpdate on the current session.

Declaration dec = Persister.findDeclarationById("id");
assertNotNull(dec.getStatusHistory()); // ERROR, is empty like before
如果我用一些条目创建一个新的Set并存储它,它一切正常,但是当我存储包含Hibernate对象的旧历史时,比如PersistSet,它将不会存储在db中... Stranggeee ....这是预期的行为吗?......我更像是一个臭虫,或者我在这里遗漏了一些东西...... 如果我调试Hibernate代码,集合条目永远不会在方法Collections.prepareCollectionForUpdate()中标记为已更新/重新创建,因为loadedPersister和currentPersister由于某种原因是相同的... 有人有什么想法吗?     
已邀请:
我相信你的测试中的最后一个判断应该断言不是空的:
assertNotNull(dec.getStatusHistory()); // ERROR, is empty like before
无论如何: 您应该使用
clear
方法而不是删除集合。 所以使用:
dec.getStatisHistory().clear();
...
assertTrue(dec.getStatusHistoryHistory.isEmpty());
代替:
dec.setStatusHistory(null);
...
assertNull(dec.getStatusHistory());
在hibernate中,删除一个hibernate mannaged集合总是一个坏主意。 在我看来,BTW甚至是一个更清晰的方法来拥有一个空集而不是null 添加 如果您真的需要删除该集合,您可以尝试在添加它之前将其内容复制到新集合中。
dec.setStatusHistory(new HashSet(history));
    

要回复问题请先登录注册