---
title: "修改带<any>关联的对象报collection was not processed by flush"
author: "DavyLee"
date: "2007-09-03T18:19:00Z"
tags: []
slug: "any-collection-was-not-processed-by-flush"
draft: false
lastmod: "2007-09-03T18:19:00Z"
id: 119907
url: "https://davylee.iteye.com/blog/119907"
---

今天修改带<any>关联的对象报错：org.hibernate.AssertionFailure:collection was not processed by flush()！涉及到了3个对象，一个企业，一个产品，一个收藏，其中收藏里通过<any>关联着企业和产品，是对产品和企业的收藏，映射文件如下：
-----------------------TPrettyFavorite.htm.xml---------------------
 
```

<class name="TPrettyFavorite" table="cn_list_TPrettyFavorite">
  <id name="id" type="integer" column="id">
    <generator class="native"/>
  </id>
  <property name="name" column="name" type="string" not-null="false" length="255"/>
  <property name="content" column="content" type="text"/>
  <any name="favorite" meta-type="string" id-type="integer" cascade="none" lazy="false">
    <meta-value value="product" class="cn.product.TPrettyProduct"/>
    <meta-value value="enterprise" class="cn.enterprise.TPrettyEnterprise"/>
    <column name="favoriteType"/>
    <column name="favoriteId"/>
  </any>
</class>

```

TPrettyProduct和TPrettyEnterpirse的映射文件从略。
下面是处理修改TPrettyFavorite对象的代码：

```

//temp是从页面传递过来的TPrettyFavorite对象
IPrettyFavorite temp = (IPrettyFavorite) this.getTableObject();
//article是从数据库里根据temp的id load出来的TPrettyFavorite对象
IPrettyFavorite article = (IPrettyFavorite) this.getCurrentTemplate.load(TPrettyFavorite.class,new Integer(temp.getId()));
article.setContent(temp.getContent());
this.getCurrentTemplate().setFlushMode(HibernateTemplate.FLUSH_ALWAYS);
this.getCurrentTemplate().saveOrUpdate(article);
this.getCurrentTemplate().flush();
this.getCurrentTemplate().evict(article);

```

下面是日志里打出来的信息：
2007-09-03 18:53:52,781 ERROR  - <an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)>
org.hibernate.AssertionFailure: collection was not processed by flush()
	at org.hibernate.engine.CollectionEntry.postFlush(CollectionEntry.java:144)
	at org.hibernate.event.def.AbstractFlushingEventListener.postFlush(AbstractFlushingEventListener.java:305)
	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:28)
	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
	at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:815)
	at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
	at org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:813)