---
title: "hibernate 中批量数据操作"
author: "mjbb"
date: "2010-03-07T21:09:19Z"
tags: []
categories: ["企业架构"]
slug: "hibernate"
draft: false
lastmod: "2010-03-07T21:09:19Z"
id: 609786
url: "https://mjbb.iteye.com/blog/609786"
---

s = sf.openSession();
  <span style="color:red"> //s.setFlushMode(FlushMode.NEVER);
   只有在flush的时候才提交数据
   flush 默认情况是在commit()时候才执行的</span>
   tx = s.beginTx();
   Customer c = null;
   for(int i = 0 ; i < 100000 ;  i++){
   	 c = new Customer();
     c.setXXX(...);
     //...
     s.save(c);
     if((i % 2000) == 0){
        <span style="color:red"> s.flush();
         s.clear();</span>
     }
   }
  <span style="color:red"> s.flush();//************ 不加该方法会丢失数据.</span>
   tx.commit();
   s.close();