Hibernate three transitions of state

Source: Internet
Author: User

Status of Hibernate
Hibernate's various preservation methods (Save,persist,update,saveorupdte,merge,flush,lock) and three states of the object
Hibernate save
Hibernate offers too many ways to save objects, and there are a lot of differences between them.
First, the preparation of knowledge
Before all, for Hibernate, its objects have three states, transient "Free State", persistent "persistent state", detached "Free State"
Below is a common translation approach:
Transient: transient or free-form
(New Deptpo (1, "Administrative department", 20, "administrative related"), the instance of the PO is not associated with the session, and the instance of the PO is in transient)
Persistent: Persistent state
(and the database records the PO instance that you want to insinuate, its state is persistent, the objects obtained by get and load are all persistent)
Detached: off-tube or Free State
(1) When the PO objects obtained through the Get or load methods are persistent, but if the delete (PO) is executed (but the transaction cannot be performed), the PO status is detached, (indicating and session out of association), A Free state due to delete can be persisted by Save or saveorupdate ()
(2) When the session is closed, the persistent PO object in the session cache becomes detached
A free state can be changed into a persistent state by lock, save, and update, because the session is closed.
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 lock () or replicate ().

Save () and persist () will cause SQL Insert,delete () to raise Sqldelete,
The update () or merge () throws a SQL update. Modifications to a persisted (persistent) instance are detected when the commit is refreshed, and it also causes SQL UPDATE.
Saveorupdate () or replicate () will cause sqlinsert or update
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 a-> object after a property change call Update
The operation steps for lock are:
(2) Call lock to change the unmodified object from the off-state to a persistent state--> the contents of the object that changed the persisted state--> wait for flush or manual flush
Viii. the difference between clear and evcit
Clear full Erase session cache
Evcit (obj) empties a persisted object from the session's cache.


Session.lock (Xtyhb,lockmode.none);//means to go directly to the cache to find the object that becomes persistent state

Session.lock (Xtyhb,lockmode.read);//The ID of the record is first read through the ID of the database to see if there is a record, if there is a subsequent to the cache to find the object becomes persistent state

Hibernate three transitions of state

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.