Differences between hibernate Storage Methods

Source: Internet
Author: User

Save hibernate
Hibernate provides too many methods for Object Storage. There are many differences between them. Let's talk about the differences here:
I. Prerequisites:
Before that, it should be noted that for hibernate, its objects have three states: transient, persistent, and detached.
Below are common translation methods:
Transient: transient or free state
Persistent: Persistence
Detached

Instances in the Free State can be persisted by calling the save (), persist (), or saveOrUpdate () method.
A persistent instance can be detached by calling delete. The instances obtained through the get () or load () method are in a persistent state.
You can call update (), 0 saveOrUpdate (), lock (), or replicate () for persistence of an instance in the unmanaged state.

Save () and persist () will trigger SQL INSERT, delete () will trigger SQLDELETE,
Update () or merge () will trigger SQLUPDATE. Modifications to persistent instances are detected during refresh and submission,
It also causes SQLUPDATE. SaveOrUpdate () or replicate () will cause SQLINSERT or UPDATE

Ii. Differences between save and update
The reason for putting this pair first is that this pair is the most commonly used.
The function of save is to save a new object.
Update is to save an object in the unmanaged state.

Iii. Differences between update and saveOrUpdate
This is easy to understand. As the name suggests, saveOrUpdate basically combines save and update.
Reference a section in the hibernate reference to explain their usage scenarios and differences
Generally, update () or saveOrUpdate () is used in the following scenarios ():
The program loads objects in the first session.
This object is passed to the presentation layer.
Some changes have been made to the object.
This object is returned to the business logic layer.
The program calls the update () method of the second session to persist these changes.

SaveOrUpdate () Does the following:
If the object is already persistent in this session, do not do anything
If another object associated with this session has the same persistent identifier (identifier), an exception is thrown.
If the object does not have the identifier attribute, call save ()
If the object's persistent identifier (identifier) indicates that it is a new instantiated object, it calls save ()
If the object contains version information (via <version> or <timestamp>) and the value of the version attribute indicates that it is a new instantiated object, save () It.
Otherwise, update () is the object.

Iv. Differences between persist and save
This is the most distinct pair. On the surface, you can use either of them. In the hibernate reference document, there is no clear distinction between them.
A clear distinction is given here. (You can follow up with src and check that although the implementation steps are similar, there are still slight differences)
---------------------------------------------------------------------------------
I found that a lot of people have the same doubt. To help to solve this issue
I'm quoting Christian Bauer:
"In case anybody finds this thread...

Persist () is well defined. It makes a transient instance persistent. However,
It doesn't guarantee that the identifier value will be assigned to the persistent
Instance immediately, the assignment might happen at flush time. The spec doesn't say
That, which is the problem I have with persist ().

Persist () also guarantees that it will not execute an INSERT statement if it is
Called outside of transaction boundaries. This is 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 an identifier, and if an INSERT
Has to be 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 is not good in a long-running conversation with an extended Session/persistence context ."

---------------------------------------------------------------------------------
Briefly translate the main content of the above sentence:
1. persist persists a transient instance, but the identifier is not guaranteed to be immediately entered into the persistent instance. The identifier may be delayed.
The time to flush.

2. persist "guarantee". This function is useful when it is called outside a transaction and does not trigger an SQL Insert statement,
When we encapsulate a long Session flow by inheriting Session/persistence context, a function like persist is required.

3. save "does not guarantee" 2nd. It must return an identifier, so it will immediately execute SQL insert, whether inside or outside the transaction


5. Differences between saveOrUpdateCopy, merge, and update
First, merge is used to replace saveOrUpdateCopy.

Then compare update and merge
The role of update is mentioned above. Here we will talk about merge's
If an instance with the same persistence identifier (identifier) exists in the session, use the object state provided by the user to overwrite the old persistent instance.
If the session does not have a persistent instance, load the instance from the database or create a new persistent instance.
The object given by the user is not associated with the session, and it is still out of control.
The focus is on the last sentence:
When update is used, after the execution is complete, the state of object A provided becomes persistent.
But when we use merge, the execution is complete, and the object A we provide is in the unmanageable state, hibernate or new B, or the retrieved
A persistent object B, and copy all the values of object A we provide to this B. After the execution is complete, B is in the persistent state, and the we provide is in the managed State.

Vi. Differences between flush and update
These two differences are easy to understand.
The update operation is performed on objects in the unmanaged state.
Flush is the object in the persistent state of the operation.
By default, objects in a persistent State do not need to be updated. As long as you change the object value, it will automatically wait for hibernate to flush.
Saved to the database. When hibernate flush occurs again:
1. When calling some queries
2. When transaction commit
3. When you manually call flush

7. Differences between lock and update
Update changes an object that has been changed to a persistent state.
Lock changes an object that has not been changed to the persistent state.
Change the content of a record. The two operations are different:
The update procedure is as follows:
(1) Change the unmanaged object-> call update
The lock operation procedure is:
(2) Call lock to change the object from the unmanaged state to the persistent state --> change the content of the object in the persistent state --> wait for flush or manually flush

Author "Study Notes"

Related Article

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.