日期:2014-05-18  浏览次数:20660 次

Hibernate 批量提交怎么做?
想知道CSDN网友的答案,

算作散点分。





------解决方案--------------------
hibernate.xml中自代这个配置项
------解决方案--------------------
再贴一次hibernate的帮助
13.1. 批量插入(Batch inserts)
如果要将很多对象持久化,你必须通过经常的调用 flush() 以及稍后调用 clear() 来控制第一级缓存的大小。 

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 //20,与JDBC批量设置相同
//flush a batch of inserts and release memory:
//将本批插入的对象立即写入数据库并释放内存
session.flush();
session.clear();
}
}
 
tx.commit();
session.close();

13.2. 批量更新(Batch updates)
此方法同样适用于检索和更新数据。此外,在进行会返回很多行数据的查询时, 你需要使用 scroll() 方法以便充分利用服务器端游标所带来的好处。 

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
 
ScrollableResults customers = session.getNamedQuery("GetCustomers")
.setCacheMode(CacheMode.IGNORE)
.scroll(ScrollMode.FORWARD_ONLY);
int count=0;
while ( customers.next() ) {
Customer customer = (Customer) customers.get(0);
customer.updateStuff(...);
if ( ++count % 20 == 0 ) {
//flush a batch of updates and release memory:
session.flush();
session.clear();
}
}
 
tx.commit();
session.close();

------解决方案--------------------
我看出来了,楼主是来散分的...
------解决方案--------------------
呵呵,jf!
------解决方案--------------------
探讨
我看出来了,楼主是来散分的...

------解决方案--------------------
study
------解决方案--------------------
学习中···
------解决方案--------------------
简单的内置方法!
------解决方案--------------------
探讨
引用:
我看出来了,楼主是来散分的...
up

------解决方案--------------------
帮忙顶。
------解决方案--------------------
楼主,我帮你顶
------解决方案--------------------
4楼的代码好像可以啊,楼主可以试试,
------解决方案--------------------
接分
------解决方案--------------------
<!-- 设置批量更新处理参数 -->
<property name="jdbc.batch_size">4</property>
<property name="cache.use_second_level_cache">fale</property>

把这个写在hibernate.cfg.xml中