使用Hibernate批量插入或更新? [英] Bulk insert or update with Hibernate?

查看:59
本文介绍了使用Hibernate批量插入或更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从每日CSV文件中消耗大量数据. CSV包含约12万条记录.使用休眠模式时,这将减慢爬网速度.基本上,当使用saveOrUpdate()时,休眠似乎在每个单独的INSERT(或UPDATE)之前执行SELECT.对于使用saveOrUpdate()持久存储的每个实例,在实际的INSERT或UPDATE之前发出SELECT.我能理解为什么要这样做,但是对于批量处理而言效率低下,我正在寻找替代方法

I need to consume a rather large amounts of data from a daily CSV file. The CSV contains around 120K records. This is slowing to a crawl when using hibernate. Basically, it seems hibernate is doing a SELECT before every single INSERT (or UPDATE) when using saveOrUpdate(); for every instance being persisted with saveOrUpdate(), a SELECT is issued before the actual INSERT or a UPDATE. I can understand why it's doing this, but its terribly inefficient for doing bulk processing, and I'm looking for alternatives

我有信心性能问题在于我使用hibernate的方式,因为我得到了另一个使用本机SQL的版本(以相同的方式解析CSV),并且它围绕此运行新版本)

I'm confident that the performance issue lies with the way I'm using hibernate for this, since I got another version working with native SQL (that parses the CSV in the excat same manner) and its literally running circles around this new version)

那么,对于实际的问题,是否存在可以代替mysqls"INSERT ... ON DUPLICATE"语法的休眠方式?

So, to the actual question, does a hibernate alternative to mysqls "INSERT ... ON DUPLICATE" syntax exist?

或者,如果我选择为此执行本机SQL,是否可以在休眠事务中执行本机SQL?意思是,它将支持提交/回滚吗?

Or, if i choose to do native SQL for this, can I do native SQL within a hibernate transaction? Meaning, will it support commit/rollbacks?

推荐答案

根据配置Hibernate使用自定义存储过程,该过程使用数据库的 upsert 功能.不过,它并不漂亮.

According to an answer to a similar question, it can be done by configuring Hibernate to insert objects using a custom stored procedure which uses your database's upsert functionality. It's not pretty, though.

这篇关于使用Hibernate批量插入或更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆