---
title: "【转】Interceptor 中onFlushDirty()函数执行多次的问题（Hibernate 的一个小Bug）"
author: "wangyanlong0107"
date: "2012-07-04T15:58:42Z"
tags: []
categories: ["企业架构"]
slug: "interceptor-onflushdirty-hibernate-bug"
draft: false
lastmod: "2012-07-04T15:58:42Z"
id: 1576587
url: "https://bigdragon.iteye.com/blog/1576587"
---

最近在项目中遇到一个奇怪的问题，在使用Hibernate拦截器捕获实体变化并进行处理时，发现其中的onFlushDirty()函数执行了很多次，导致进行处理时产生很多重复数据。具体问题如下：  
使用一个类继承Hibernate的HistoryInterceptor类来对程序中的实体变化进行拦截，并在其中的onFlushDirty()函数中对捕获的数据进行处理，产生对应的event数据并保存到数据库中。 例如：  
public class EventInterceptor extends EmptyInterceptor {  
public boolean onFlushDirty(  
        Object entity,  
        Serializable id,  
        Object\[] currentState,  
        Object\[] previousState,  
        String\[] propertyNames,  
        Type\[] types ) throws CallbackException {

        if(isAuditable(entity)){//如果该实体需要被记录，生成对应的event。  
            //此处生成对应的event。  
        }  
        return false;  
    }

在程序执行后，发现对应一条实体的变化生成了多条重复的event记录，非常不解。  
后来google发现，有人也碰到过对应的问题，并在Hibernage论坛中提出过这个问题，鉴定为Hibernate的一个小Bug。  
（原文地址：http://forum.hibernate.org/viewtopic.php?t=940410&highlight=interceptor+onflushdirty）  
解决方法如下：  
将FlushMode改为：FlushMode.COMMIT  
或者也可以提前进行flush()  
都可以解决这个问题。