在休眠中插入大量记录的最佳方法
我正在使用休眠+播放!工作中的框架,是否有使用 hibernate 插入大量记录的“最佳实践”?每个文本文件大约有 6,000 到 10,000 个,所以我不知道 Hibernate 是否会在工作中卡住或抛出异常。
任何建议请告诉我,如果我需要解释更多,请告诉我
I'm using hibernate + play! framework at work, is there a "best practice" on inserting a good amount of records using hibernate? They are around 6,000 to 10,000 per text file so I don't know if Hibernate is going to choke on the job or throw an exception.
Any suggestion let me know, let me know if I have to explain more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自 *Java Persistence and Hibernate" (Manning) 并遵循 Pangea 的评论,使用无状态会话(没有持久性上下文缓存):
From *Java Persistence and Hibernate" (Manning) and following a comment from Pangea, use a stateless session (which doesn't have a persistence context cache) :
只是对 Kartoch 的答案中的代码进行了一些更正。
根据 批处理,“插入StatelessSession 接口定义的 ()、update() 和 delete() 操作被认为是直接数据库行级操作,它们分别导致 SQL INSERT、UPDATE 或 DELETE 的立即执行。它们与保存具有不同的语义。 ()、saveOrUpdate() 和 delete() 操作由 Session 接口定义。”
StatelessSession 不再需要 save()、flush()、clear()。代码应该是这样的:
最后,这里讨论一下普通批量插入和StatelessSession插入之间的区别:使用 StatelessSession 进行批处理。
Just some corrections of the codes in the answer of Kartoch.
According to Batch Procession, "The insert(), update() and delete() operations defined by the StatelessSession interface are considered to be direct database row-level operations. They result in the immediate execution of a SQL INSERT, UPDATE or DELETE respectively. They have different semantics to the save(), saveOrUpdate() and delete() operations defined by the Session interface."
No more save(), flush(), clear() for StatelessSession. The code should be like this :
Finally, Here is a discussion of the difference between the normal batch inserting and the StatelessSession insert : Using StatelessSession for Batch processing.
最好是使用 StatelessSessions。考虑以下示例 (http://docs .jboss.org/hibernate/orm/3.3/reference/en-US/html/batch.html):
Best is to use StatelessSessions. Consider the following example from (http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/batch.html):
只需打开您的会话和交易即可。
添加会话保存中的所有元素。
然后提交事务。
Just open your session and transaction.
Add all elements in the save of the session.
Then commit the transaction.
如果您想在休眠之外进行插入,您始终可以直接获取 Connection 对象。
You can always get a Connection object directly in case you want to do the inserts outside of hibernate.