如何使用Hibernate批处理 [英] How to use Hibernate batch processing
本文介绍了如何使用Hibernate批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对象列表,我只想使用休眠批处理保存这些对象.下面是我尝试过的代码
i have a list of objects i just want to save that objects using hibernate batch processing .below is the code that i have tried
public void create(List<TransArchive> transArchives) {
Session session = getCurrentSession();
Transaction tx = null;
tx = session.beginTransaction();
for (TransArchive transArchive : transArchives) {
session.save(transArchive);
} }
请帮助我如何在上述代码中使用批处理
please help me to how to use batch processing in above code
推荐答案
对于休眠批处理,您必须在配置文件中设置 Batch_Size 属性.
For hibernate batch processing you have to set the Batch_Size property in your configuration file.
<property name="hibernate.jdbc.batch_size"> 50 </property>
稍后,如下更新代码:
public void create(List<TransArchive> transArchives) {
Session session = getCurrentSession();
Transaction tx = null;
tx = session.beginTransaction();
for (int i=0;i<transArchives.size();i++) {
//save the object
session.save(transArchives.get(i));
if( i % 50 == 0 ) // Same as the JDBC batch size
{
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();
}
这是因为默认情况下,Hibernate将所有持久化的对象缓存在会话级缓存中,最终您的应用程序将崩溃,并出现 OutOfMemoryException .
This is Because by default, Hibernate will cache all the persisted objects in the session-level cache and ultimately your application would fall over with an OutOfMemoryException.
这篇关于如何使用Hibernate批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文