Three state transitions of the object after hibernate increase and change

Source: Internet
Author: User

This.getsession (). Update (obj);

This.getsession (). Merge (obj);

This.getsession (). saveorupdate (obj);
1. The difference between update and merge

The first time you perform an update operation, both must have an ID

Update executes the UPDATE statement directly, and the state is persisted after execution

And the merge is different:

1. If there is a corresponding object in the session, which is the same as the primary key, the value of obj to be saved is copied to the object in the session, and then the object in the session after the update is updated

2. If there is not a session, then the object that is checked out by the update will be copied from the database by the select and then the obj.

3. So merge will select and update

4. So the original object of the merge after the obj state is off-pipe.

================ below for excerpt =============

Second, save and update differences
The reason for putting this pair first is because the pair is the most common.
The role of Save is to save a new object
Update is a de-state object or a Free state object (be sure to correspond to a record) to the database

Iii. Update and Saveorupdate differences
This is a good understanding, as the name implies, Saveorupdate basically is the synthesis of save and update, and update is only update; Refer to a paragraph in Hibernate reference to explain their use and differences
Typically the following scenario uses update () or saveorupdate ():
The program loads the object in the first session and then closes the 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 eventually to the persistence layer
The program creates a second session call to the second session of the update () method to persist these changes

Saveorupdate (PO) do the following:
If the PO object is already persisted in this session, perform saveorupdate in this session without doing anything
If the Savaorupdate (New PO) has the same persistent identity (identifier) as another PO object associated with this session, an exception is thrown
Org.hibernate.nonuniqueobjectexception:a different object with the same identifier value is already associated with the session: [Org.itfuture.www.po.xtyhb#5]
Saveorupdate if the object does not have a persisted identity (identifier) attribute, call Save () on it, otherwise the update () object

Iv. differences between persist and save
This is the most blurred pair, which appears to be used on the surface, and there is no clear distinction between them in the Hibernate reference documentation.
A clear distinction is given here. (You can follow SRC to see, although the implementation steps are similar, but there are subtle differences)
Main content differences:
1,persist persists a transient instance, but does not guarantee that the identifier (the attribute corresponding to the identifier primary key) is immediately populated into the persisted instance, and that the identifier filling may be deferred until flush.

2,save, the persistent identifier of a transient instance, is generated in a timely manner, it returns an identifier, so it executes the SQL insert immediately

V. Saveorupdate,merge and UPDATE differences
Compare update and Merge
The role of update says, "Here's a look at the merge
If there is an instance of the same persistent identity (identifier) in the session, overwrite the persisted instance of the session with the object given by the user
(1) When we use update, we will throw an exception when we finish the execution.
(2) But when we use the merge, we copy the properties of the Po object A that handle the free State to the properties of the PO that is in the persistent state in the session, whether it is persistent or persistent after execution, and we provide a or a free State

Six, flush and update differences
The difference between the two is good understanding
The update operates on an object that is in a free State or in a de-//updatesql state (in the off-pipe state due to the closing of the session).
While flush is an object that operates on a persisted state.
By default, changes to a persistent state object (including the set container) do not require an update, as long as you change the value of the object, waiting for Hibernate flush to automatically update or save to the database. Hibernate flush occurs in the following situations:
1, call some queries and manual flush (), session closed, Sessionfactory closed combined
Get () An object that changes the properties of the object and closes the resource.
2,transaction commit (contains flush)

Seven, lock and update differences
Update is to turn an object that has changed out of the tube state into a persistent state
Lock is a persistent state of a non-changed off-state object (for a PO object (2) that is in the de-state due to the closing of the session, and not for a PO object that is in a de-state due to delete)
Corresponding to change the contents of a record, two different operations:
The procedure for update is:
(1) Modification of the off-tube object after a property change, call Update
The operation steps for lock are:
(2) Call lock to change an unmodified object from the off-pipe state to a persistent state--changing the contents of the persisted state--waiting for flush or manually flush
Viii. the difference between clear and evcit
Clear full Erase session cache
Evcit (obj) empties a persisted object from the session's cache.

Three state transitions of the object after hibernate increase and change

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.