【问题标题】:what is difference between hibernateTemplate Flush() and clear() methods?hibernateTemplate Flush() 和 clear() 方法有什么区别?
【发布时间】:2013-05-29 08:28:00
【问题描述】:

hibernateTemplate flush() 和 clear() 方法有什么区别?什么时候用?

我有以下代码

    public void saveAllReportRoomRes(List<ReportRoomRes> reportRoomResList) throws  DataAccessException {
    hibernateTemplate.saveOrUpdateAll(reportRoomResList);
    hibernateTemplate.flush();
    }

我在循环中调用此方法,每次传递记录列表(总记录可以超过 2 个 lacs)。因为我的代码在事务中,有时在后续迭代中,如果休眠找到相同的主键(我有复合主键)对象,它会抛出 NonUniqueObject 异常。但实际上它需要更新之前插入的记录。

如果我使用 HibernateTemplate.clear(),它可以工作。

这也是保存或更新大量记录的好方法吗?因为这是我通常的情况,我以一定的间隔将数据从一个数据库复制到另一个数据库。

【问题讨论】:

    标签: hibernate flush


    【解决方案1】:

    休眠 Session.clear() :

    void org.hibernate.Session.clear()

    从 Session 缓存中删除所有对象,并取消所有挂起的保存、更新和删除。

    作为 Session.flush 将所有挂起的保存、更新和删除刷新到数据库。

    http://docs.spring.io/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/HibernateTemplate.html

    选择合适的,取决于交易类型,你选择。

    再回答一个问题

    使用 Batch 更新大量记录。

    【讨论】:

    • 您说:使用批量更新大量记录。什么是批处理? Spring Batch 等其他框架??
    • 是的。我只指 Spring Batch 模板
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    相关资源
    最近更新 更多