Different ways to save Hibernate (Save,persist,update,saveorupdte,merge,flush,lock), etc.

Source: Internet
Author: User


Hibernate save
Hibernate offers too many ways to save objects, and there are a lot of differences between them, and here's a little bit of a different way:
First, the preparation of knowledge: Before all, explain that for Hibernate, its object has three states, transient, persistent, detached
Below is a common translation approach:
Transient: transient or free-form
Persistent: Persistent state
Detached: off-tube or Free State

Instances of the off-state can be persisted by calling the save (), persist (), or Saveorupdate () method.
A persisted instance can become a de-state by calling Delete (). The instances obtained through the get () or load () methods are persisted.
Instances of the off-state can be persisted by calling update (), Saveorupdate (), lock (), or replicate ().
An instance of free or free State can be a new persisted instance by calling the merge () method.

Save () and persist () will cause SQL Insert,delete () to throw a delete of SQL,

Update () or merge () raises the SQL update.

Modifications to a persisted (persistent) instance are detected when the commit is refreshed, and it also causes sqlupdate. Saveorupdate () or replicate () will cause sqlinsert or update

The difference between save and update the reason for putting this pair in the first place is because this pair is the most commonly used.
The role of Save is to save a new object
Update is to save an object that is out of the tube state
Third, update and saveorupdate distinguish this is a better understanding, as the name implies, Saveorupdate basically is the synthesis of Save and update
Refer to a passage in hibernate reference to explain their use and differences
Typically the following scenario uses update () or saveorupdate ():
The program loads an object in the first session
The object is passed to the presentation layer
There have been some changes to the object
The object is returned to the business logic layer
The program calls the update () method of the second session to persist these changes

Saveorupdate () do the following:
If the object is already persisted in this session, do nothing
Throws an exception if another object associated with this session has the same persistent identity (identifier)
If the object does not have a persisted identity (identifier) attribute, call Save () on it
If the persistent identity of the object (identifier) indicates that it is a newly instantiated object, call Save () on it
If the object is accompanied by version information (through <version> or <timestamp>) and the value of the Version property indicates that it is a newly instantiated object, save () it.
Otherwise, the update () object
Four, persist and save distinguish this is the most blurred pair, on the surface it seems to use any line, in the Hibernate reference document also does not clearly distinguish them.
A clear distinction is given here. (You can follow SRC to see, although the implementation steps are similar, but there are subtle differences)
Here is a description in http://opensource.atlassian.com/projects/hibernate/browse/HHH-1682:
---------------------------------------------------------------------------------I found that a lot of people has the Same doubt. To solve the issue I ' m quoting Christian Bauer: "In case anybody finds this thread ... persist () is the well defined. It makes a transient instance persistent. However, it doesn ' t guarantee that the identifier value would be assigned to the persistent instance immediately, the Assig Nment might happen at flush time. The spec doesn ' t say that, which is the problem I has with persist (). Persist () also guarantees that it won't execute an INSERT statement if it's called outside of transaction boundaries. This was useful in long-running conversations with an extended session/persistence context. A method like persist () is required. Save () does not guarantee the same, it returns a identifier, and if an INSERT have to is executed to get the identifier (E . G. "Identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or Outside of a transaction. This isn't good a long-running conversation with an extended session/persistence context. "---------------------------  ------------------------------------------------------

Briefly translate the main contents of the above sentence:
1,persist persists a transient instance, but does not guarantee that the identifier is immediately populated into the persisted instance, and that the identifier's fill-in May be deferred
To flush the time.

2,persist "Guaranteed" does not trigger a SQL Insert when it is called outside of a transaction, which is useful when we encapsulate a long session process by inheriting Session/persistence context , a function such as persist is needed.

3,save "does not guarantee" 2nd, it returns an identifier, so it executes the SQL insert immediately, whether inside or outside transaction
Five, the Saveorupdatecopy,merge and update differences first indicate that the merge is used to replace the saveorupdatecopy, this detailed see here
Http://www.blogjava.net/dreamstone/archive/2007/07/28/133053.html
Then compare update and merge
The role of update says, "Here's a look at the merge
If an instance of the same persistence identity (identifier) exists in the session, overwriting the old persisted instance with the state of the object given by the user
If the session does not have a corresponding persistent instance, try to load from the database, or create a new persisted instance, and finally return the persisted instance
User-given object is not associated with the session, it is still off the tube
The point is the last sentence:
When we use update, the state of the object A that we provide becomes persisted after execution is completed
But when we use the merge, the execution is done, we provide the object A is still out of the tube State, hibernate or new a B, or retrieve a persistent object B, and we provide a copy of all the values of object A to this B, after the completion of the implementation of B is a persistent state, And the A we provide is a managed state.
Six, flush and update distinguish between these two good understanding
Update operation is in the off-state object
While flush is an object that operates on a persisted state.
By default, a persistent state object is not required for update, as long as you change the value of the object, waiting for Hibernate flush to automatically
Saved to the database. Hibernate flush occurs in several cases:
1, when some queries are called
When 2,transaction commits,
3, when flush is manually called
Seven, lock and update difference update is to change an already changed off-state object into a persistent state
Lock is the permanent state of an object that has not changed the state of the de-
Corresponding to change the contents of a record, two different operations:
The procedure for update is:
(1) Change the object of the riser, call Update
The operation steps for lock are:
(2) Call lock to turn the object from the off-tube state to a persistent state--change the contents of the persisted state--wait for flush or flush manually


Different ways to save Hibernate (Save,persist,update,saveorupdte,merge,flush,lock), etc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.