使用 Hibernate 插入(更新)大量实体的最佳方法是什么?
我需要执行类似的操作
for (int i = 0; i<=moreThanThousand; i++){
Entity e = new Entity();
insertEntity(e);
}
或者
for (Entity e: moreThanThousandEntities){
updateEntity(e);
}
Hibernate 中是否有批处理机制?在多个线程中执行这项工作是否有意义?最佳实践是什么? 对于 JDBC,我会使用PreparedStatement 的addBatch() 和executeBatch() 方法,但我不是Hibernate 专家。 提前致谢!
I I need to execute something like
for (int i = 0; i<=moreThanThousand; i++){
Entity e = new Entity();
insertEntity(e);
}
or
for (Entity e: moreThanThousandEntities){
updateEntity(e);
}
Is there some batch mechanism in Hibernate? Does it make sense to perform this work in several threads? What is the best practice?
With JDBC I would use addBatch() and executeBatch() methods of PreparedStatement but I'm not expert in Hibernate.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用以下方法定义批量大小:
批量插入/更新很简单:
有关更多详细信息,请查看 此处。
You can define the batch size using:
The inserting/updating in batches is easy:
For more details, have a look here.
如果您正在处理大量数据,例如每天导入数据,并且您的处理窗口非常小,那么不幸的是,最好的方法是使用 JDBC 直接访问数据库,请考虑所有这些:
我可以告诉你,在系统上,我们每天应该处理大约 800 万条记录(我没有以字节为单位,但很大),每天只有 2 小时,因此这是达到最佳性能的唯一方法,即使使用允许的最高硬件。
我希望我为您提供了一种可供考虑的新的有用方法。
If you are processing a great volume of data, like as importing data every day and you have a very small window of processing to do that, the best approach unfortunately is to access your DB directly using JDBC, consider all these:
I can tell you that on systems when we should process about 8 millions of records (I haven't the volume in bytes but is big) daily during only 2 hours by day, thus was the only way to reach the best performance even using the highest hardware allowed.
I hope i have gave to you a new useful approach to consider.