hibernate 批量插入

Session session = sessionFactoryUpLowLimit.openSession();
session.beginTransaction();

for(int i=0 ;i< xx;i++)

{

 session.save(upperLowerLimitDataBean);

if(i%2==0){ try{
session.flush();
session.clear();
session.getTransaction().commit();  //一定要加   如果不提交事务,数据将依然缓存在事务处——未进入数据库,也将引起内存溢出的异常。
session.beginTransaction();//一定要加   
}catch(Exception e) {System.out.println("session--flush--error:"+e.getMessage());}}

}

try{ session.getTransaction().commit(); }catch(Exception e) {System.out.println("session--commit--error:"+e.getMessage());} //最后提交一次 

session.close();//关闭session

原文地址:https://www.cnblogs.com/lj821022/p/7072172.html