Hibernate saveOrUpdate大数据 [英] Hibernate saveOrUpdate large data
问题描述
我试图用Hibernate插入或更新大量数据。我有一个包含350k对象的列表,当我使用Hibernate saveOrUpdate()
时,插入所有数据需要几个小时。
I am trying to insert or update a large data with Hibernate. I have a list contains 350k objects and when I use Hibernate saveOrUpdate()
, it takes hours to insert all the data.
我正在使用下面的代码进行此操作。我的开发环境是JDK1.4和Oracle数据库。
I am using the code below for this operation. My development environment is JDK1.4 and Oracle Database.
public void saveAll(List list)throws HibernateException{
Session session = HibernateUtil.getEYSSession();
Iterator it = list.iterator();
int i = 0;
while(it.hasNext()){
i++;
Object obj = it.next();
session.saveOrUpdate(obj);
if (i % 50 == 0) { session.flush(); session.clear(); }
}
}
我正在使用批量更新并设置 hibernate.jdbc.batch_size
属性50但它没有帮助。
I am using batch update and also set hibernate.jdbc.batch_size
property 50 but it didn't help.
我的对象与一对一的关系在这种情况下使用StatelessSession的另一个对象可能会成为问题。因为StatelessSession不会级联到我认为的组合对象。
My object has one-to-one relation with another object so in this case using StatelessSession might be a problem. Because StatelessSession does not cascade to composed objects I think.
您对如何提高 saveOrUpdate()
在这种情况下的操作?
Do you have any ideas about how to increase the performance of saveOrUpdate()
operation in this case?
谢谢。
Thanks.
推荐答案
对于每一个 saveOrUpdate
操作,Hibernate也会生成一个 select
语句。在我看来,使用Hibernate的saveOrUpdate更新或插入大量数据并不是一个好的选择。我建议使用存储过程或移动到SpringJDBCTemplate。
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.
这篇关于Hibernate saveOrUpdate大数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!