简体   繁体   English

休眠-使用save()方法进行差异更新与使用update()方法进行差异更新

[英]Hibernate - difference update using save() method vs. update using update() method

I have an simple Hibernate Project where I update an existing Object using the save method. 我有一个简单的Hibernate项目,其中使用save方法更新了一个现有Object。

So the following code works for me. 因此以下代码对我有用。

sessionObj.beginTransaction();
MyObject myObj = sessionObj.get(MyObject.class, objID);
myObj.setPropertyA("new value");
sessionObj.save(myObj);
sessionObj.getTransaction().commit(); 

I obtain the Object with the ID objID via get() method, change a value, mark it as persistent using save() method and commit the transaction. 我通过get()方法获得ID为objID的Object,更改值,使用save()方法将其标记为持久并提交事务。 I observed that Hibernate generates an UPDATE Statement in this case. 我观察到Hibernate在这种情况下会生成UPDATE语句。

So if I can make an UPDATE this way using save() method whats the difference make an update using the update() method? 因此,如果我可以使用save()方法以这种方式进行UPDATE,则使用update()方法进行更新有何不同?

Like 喜欢

MyObject myObj = sessionObj.get(MyObject.class, objID);
myObj.setPropertyA("new value");
sessionObj.update(myObj);

SAVE (method) 保存(方法)

The save method is an “original” Hibernate method that does not conform to the JPA specification. save方法是一种不符合JPA规范的“原始”休眠方法。

Its purpose is basically the same as persist, but it has different implementation details. 它的目的与persist基本相同,但是实现细节不同。 The documentation for this method strictly states that it persists the instance, “first assigning a generated identifier”. 该方法的文档严格声明其保留实例,“首先分配生成的标识符”。 The method is guaranteed to return the Serializable value of this identifier. 保证该方法返回此标识符的Serializable值。

Person person = new Person();
person.setName("John");
Long id = (Long) session.save(person);

The effect of saving an already persisted instance is the same as with persist. 保存已经持久化的实例的效果与持久化的效果相同。 Difference comes when you try to save a detached instance: 尝试保存分离的实例时会有所不同:

Person person = new Person();
person.setName("John");
Long id1 = (Long) session.save(person);

session.evict(person);
Long id2 = (Long) session.save(person);

The id2 variable will differ from id1. id2变量将不同于id1。 The call of save on a detached instance creates a new persistent instance and assigns it a new identifier, which results in a duplicate record in a database upon committing or flushing. 在分离的实例上调用save会创建一个新的持久性实例,并为其分配一个新的标识符,这将在提交或刷新后在数据库中产生重复的记录。

UPDATE (method) 更新(方法)

As with persist and save, the update method is an “original” Hibernate method that was present long before the merge method was added. 与persist and save一样,update方法是一种“原始” Hibernate方法,在添加merge方法之前很久就存在。 Its semantics differs in several key points: 其语义在几个关键点上有所不同:

it acts upon passed object (its return type is void); 它对传递的对象起作用(其返回类型为void); the update method transitions the passed object from detached to persistent state; update方法将传递的对象从分离状态转换为持久状态; this method throws an exception if you pass it a transient entity. 如果将瞬态实体传递给此方法,则此方法将引发异常。 In the following example we save the object, then evict (detach) it from the context, then change its name and call update. 在下面的示例中,我们保存对象,然后将其从上下文中逐出(分离),然后更改其名称并调用update。 Notice that we don't put the result of the update operation in a separate variable, because the update takes place on the person object itself. 注意,我们没有将更新操作的结果放在单独的变量中,因为更新发生在person对象本身上。 Basically we're reattaching the existing entity instance to the persistence context — something the JPA specification does not allow us to do. 基本上,我们是将现有的实体实例重新附加到持久性上下文上-JPA规范不允许我们这样做。

Person person = new Person();
person.setName("John");
session.save(person);
session.evict(person);

person.setName("Mary");
session.update(person);

Trying to call update on a transient instance will result in an exception. 尝试在瞬态实例上调用update将导致异常。 The following will not work: 以下内容不起作用:

Person person = new Person();
person.setName("John");
session.update(person); // PersistenceException!

For more information please folow this link 有关更多信息,请点击此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM