本文介绍了JPA合并与持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直希望始终使用EntityManager的 merge()来处理插入和更新。但我也注意到merge在update / insert之前执行了额外的select查询,以确保数据库中不存在记录。

So far, my preference has been to always use EntityManager's merge() take care of both insert and update. But I have also noticed that merge performs an additional select queries before update/insert to ensure record does not already exists in the database.

现在我正在处理一个项目需要对数据库进行大量(批量)插入。从性能的角度来看,在我绝对知道我总是创建一个要保留的对象的新实例的场景中使用persist而不是merge是有意义的吗?

Now that I am working on a project requiring extensive (bulk) inserts to the database. From a performance point of view does it make sense to use persist instead of merge in a scenario where I absolutely know that I am always creating a new instance of objects to be persisted?

推荐答案

persist 足够时,使用 merge 并不是一个好主意 - merge 做了相当多的工作。该主题之前已经,并且详细解释了这些差异,并带来了一些不错的流程图表使事情变得清晰。

It's not a good idea using merge when a persist suffices - merge does quite a lot more of work. The topic has been discussed on StackOverflow before, and this article explains in detail the differences, with some nice flow diagrams to make things clear.

这篇关于JPA合并与持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:57