简体   繁体   English

如何使用Hibernate批处理

[英]How to use Hibernate batch processing

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 我有一个对象列表,我只想用hibernate批处理保存这些对象。下面是我试过的代码

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 请帮我看看如何在上面的代码中使用批处理

For hibernate batch processing you have to set the Batch_Size property in your configuration file. 对于hibernate批处理,您必须在配置文件中设置Batch_Size属性。

<property name="hibernate.jdbc.batch_size"> 50 </property>

Later on, update your code as below: 稍后,更新您的代码如下:

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();
}

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将缓存会话级缓存中的所有持久对象,最终您的应用程序将因OutOfMemoryException而崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM