使用 JPA 和 Hibernate 进行高效批量/批量插入的最终方法?
虽然这里还有其他类似的问题,但我没有看到任何解决所有问题或有一个很好的明确答案。本质上,我正在使用 Hibernate 支持的 JPA 在 J2EE 应用程序中构建数据访问和服务层。
该应用程序执行大量数据加载/更新操作,我想确保这些操作尽可能有效地进入数据库。正确的答案将用代码解释如何使用大量数据进行插入、更新和理想合并
- 在配置或受尊重的代码中设置批量大小,以及如何使用 JPA 对其进行编码(不使用原始 Hibernate如果可能)
- 如何以及何时使用 JPA 事务命令/注释以确保高效的内存/一级/二级缓存使用。
- 向我解释一下这是什么意思“如果您使用身份标识符生成器,Hibernate 会透明地禁用 JDBC 级别的插入批处理”——这与使用主标识符序列有关吗?
- 我应该知道的任何问题。
注意我已经问了一些有关 Hibernate 和 J2EE/JPA 的相关问题,如果您有任何要添加的内容,请执行这
技术对我来说都是相对较新的技术(请参阅我的其他问题):
两种 com/questions/7856367/how-should-entitymanager-be-used-in-a-nicely-de Coupled-service-layer-and-data-ac">如何在良好解耦中使用 EntityManager服务层和数据访问层?
和
While there are other similar questions here, I didn't see any that address all the issues or have a good definitive answer. Essentially I am working on architecting data access and service layers in a J2EE app using Hibernate backed JPA.
The application does large data loading/update operations, and I want to make sure these get into the database as efficiently as possible. Correct answer will explain, with code, how to consume a large collection of data for insert, update, and ideally merge
- Set a batch size in configuration or in code that is respected, and how to code to that using JPA ( without using raw Hibernate if possible)
- how and when to use the JPA transactional commands / annotations to ensure efficient memory / first / second level cache use.
- Explain to me what this means 'Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator' - is this related to using a sequence for primary identifiers?
- any gotchas I should know about.
Note I've asked some related questions about Hibernate and J2EE/JPA and if you have anything to add to these please do
Both are relatively new technologies to me (see my other questions):
How should EntityManager be used in a nicely decoupled service layer and data access layer?
and
Should raw Hibernate annotated POJO's be returned from the Data Access Layer, or Interfaces instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以解释有关 Hibernate 在使用身份生成器时禁用批量插入的声明。
为了让 Hibernate 使用身份生成器获取新实体的标识符,它必须实际执行对数据库的插入,然后执行选择来获取该标识符值,因为该值是由数据库在插入时分配的。这与使用序列生成器形成对比。在这种情况下,Hibernate 可以预先获取尽可能多的标识符(如果需要的话可以批量获取),并在插入之前将它们分配给要插入的实体。
因此,区别在于用于身份生成器的“插入然后选择”与用于序列的“选择然后插入”。
因此,当使用身份生成器时,Hibernate 必须一项一项地执行插入,但当使用序列生成器时,Hibernate 可以将它们批量化。
I can explain the statement about Hibernate disabling Batch Inserts when an identity generator is used.
In order for Hibernate to obtain an identifier for a new entity using an identity generator it must actually perform the insert to the database and then perform a select to obtain that identifier value since the value is assigned by the database on Insert. This is in contrast to using a sequence generator. In this case Hibernate can obtain as many identifiers up front (in batches if requires) and assign them to the entities it is inserting before they are inserted.
So the difference is Insert then Select for identity generator vs Select then Insert for a sequence.
Therefore Hibernate must do the Inserts one by one when an identity generator is used but can batch them up when a sequence generator is used.