首页 > 其他 > 详细

hibernate 批量插入 Batch

时间:2014-01-26 15:50:49      阅读:330      评论:0      收藏:0      [点我收藏+]

批量插入(Batch inserts)
如果要将很多对象持久化,你必须通过经常的调用 flush()以及稍后调用 clear()来控制第一级缓存的大小。

1
2
3
4
5
6
7
8
9
10
11
12
13
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
  Customer customer = new Customer(.....);
  session.save(customer);
  if ( i % 20 == 0 ) { //20, same as the JDBC batch size
    //flush a batch of inserts and release memory:
    session.flush();
    session.clear();
  }
}
tx.commit();
session.close();

  

INSERT语句的伪码是:INSERT INTO EntityName properties_list select_statement。要注意的是:
?只支持 INSERT INTO ... SELECT ... 形式,不支持 INSERT INTO ... VALUES ... 形式。

bubuko.com,布布扣
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String hqlInsert = "insert into DelinquentAccount (id, name) select c.id, c.name from Customer
c where ...";
int createdEntities = s.createQuery( hqlInsert )
.executeUpdate();
tx.commit();
session.close();
bubuko.com,布布扣

hibernate 批量插入 Batch

原文:http://www.cnblogs.com/siyu/p/3533936.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!