Hibernate saveOrUpdate large data

百般思念 提交于 2019-12-04 21:29:42

For every saveOrUpdate operation Hibernate generates a select statement as well. In my opinion updating or inserting huge data using Hibernate's saveOrUpdate is not at all a good option. I would suggest using either a stored procedure or move to SpringJDBCTemplate.

I found that separating the instances where the save() method or the Hibernate merge() had to be used provided slightly better performance. But, I remember saving and updating 100,000 objects and it also took quite a long time. For that reason a stored procedure may be the more efficient mechanism.

You can use futureTask to complete your database inserting. and continue your program.

This will not reduce the time but program doesn't wait for completing insert process.

  Session session = sessionFactory.openStatelessSession();
  Transaction tx = session.beginTransaction();
  int i = 0;
  while(it.hasNext()){ 
    i++;
    Object obj = it.next();
    session.save(obj) 
    if (i % 50 == 0) { session.flush(); session.clear();}

statelessSession will help to increase performance.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!