简体   繁体   中英

Why after update my data should flush using Hibernate?

I am check a project's dao which is colleague send to me. And I find update method:

@Override
public void update(Object entity) {

    try {
        getHibernateTemplate().update(entity);
        getHibernateTemplate().flush();
        if (logger.isInfoEnabled()) {
            logger.info("update entity success,"+entity.getClass().getName());
        }
    } catch (RuntimeException e){
        logger.error("update entity fail," + entity.getClass().getName(), e);
        throw e;
    }
}

You know, after update entity, why should invoke getHibernateTemplate().flush() ? this step is for what?

You should know the Hibernate's cache mechanism. When you query your entity, it get the data from database, but if the second time to get the same data, it will not from the database, but from the cache.

If use flush() , means refresh the cache. So the next time to get the data is from database.

If you use flush it instantly flush all pending saves, updates and deletes to the database.

Only invoke this for selective eager flushing, for example when JDBC code needs to see certain changes within the same transaction. Else, it is preferable to rely on auto-flushing at transaction completion. Whereas other method such persist and save will save the data at the completion of the current transaction.

Reference http://docs.spring.io/spring-framework/docs/2.5.x/api/org/springframework/orm/hibernate3/HibernateOperations.html#flush()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM