简体   繁体   中英

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.

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. I observed that Hibernate generates an UPDATE Statement in this case.

So if I can make an UPDATE this way using save() method whats the difference make an update using the update() method?

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.

Its purpose is basically the same as persist, but it has different implementation details. 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.

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. 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.

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. Its semantics differs in several key points:

it acts upon passed object (its return type is void); the update method transitions the passed object from detached to persistent state; 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. 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. Basically we're reattaching the existing entity instance to the persistence context — something the JPA specification does not allow us to do.

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. The following will not work:

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

For more information please folow this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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