Identify the three states of Hibernate, and save, update, saveOrUpdate, merge, etc.

Source: Internet
Author: User
Keyword: hibernate

Identify the three statuses of Hibernate, and use of save, update, saveOrUpdate, merge, etc.
Hibernate objects have three states: Transient, Persistent, and Detached ). Objects in the persistent State are also known as PO (Persistence Object), and transient objects and unmanaged objects are also known as VO (Value Object ).
Instantaneous State
Use the new command to open up java objects in the memory space,
Eg. Person person = new Person ("xxx", "xx ");
If there is no variable to reference this object, it will be recycled by the Java Virtual Machine.
An instantaneous object exists in an isolated memory. It is a carrier carrying information and does not have any association with the database data. In Hibernate, you can save () or saveOrUpdate () through the session () method: Associate the instantaneous object with the database and insert the corresponding data into the database. At this time, the instantaneous object is converted into a persistent object.
Persistent state
Objects in this state have corresponding records in the database and a persistent identifier. If the delete () method of hibernate is used, the corresponding Persistent object becomes an instantaneous object. Because the corresponding data in the database has been deleted, the object is no longer associated with the database records.
After a session executes close (), clear (), and evict (), the persistent object becomes a pipe-dropping object, and the persistent object becomes a pipe-dropping object, although the object has a database recognition value, it is no longer managed by the HIbernate Persistence Layer.
Persistent objects have the following features:
1. associate with the session instance;
2. There are associated records in the database.
Off-pipe status
When the session associated with a persistent object is closed, the persistent object is converted to a unmanaged object. When the unmanaged object is re-associated to the session, it is converted into a persistent object again.
The unmanaged object has the recognition value of the database. It can be converted to a persistent object through update (), saveOrUpdate (), and other methods.
The pipe dropping object has the following features:
1. Essentially, it is the same as an instantaneous object. When no variable references it, JVM recycles it as appropriate;
2. A database record ID value is more than the instantaneous object.

Save, persist, update, saveOrUpdte, merge, flush, lock, and object states of hibernate
Save hibernate
Hibernate provides too many methods for Object Storage. There are many differences between them. Let's take a closer look at the differences.
I. Prerequisites
For hibernate, its object has three states: transient, persistent, detached
Below are common translation methods:
Transient: transient or free state
(New DeptPo (1, "Administrative Department", 20, "Administrative related"), the po instance is not associated with the session, and the po instance is in the transient)
Persistent: Persistence
(It records the desired Po instance in the database. Its status is persistent, and the objects obtained through get and load are persistent)
Detached
(1) When the po objects obtained through the get or load method are all in the persistent State, but if the delete (po) is executed (but the transaction cannot be executed), the po state is in the detached state, (disassociation with the session). Changes to the Free State due to delete can be changed to the persistent State through save or saveOrUpdate ().
(2) When the session is closed, the po object of the persistent in the session cache is also changed to detached.
If the session is closed and becomes a free state, it can be changed to a persistent State through lock, save, and update.
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 lock () or replicate () to persist an instance in the unmanaged status.
Save () and persist () will trigger SQL INSERT, delete () will trigger SQLDELETE,
However, update () or merge () triggers SQL UPDATE. Modifications to persistent instances are detected during refresh and submission, which also results in SQL UPDATE.
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 update an unmanaged or free-State object (must correspond to a record) to the database.
Iii. Differences between update and saveOrUpdate
This is easy to understand. As the name suggests, saveOrUpdate basically combines save and update, while update is only update. reference a section in hibernate reference to explain their usage and differences.
Generally, update () or saveOrUpdate () is used in the following scenarios ():
The program loads objects in the first session and closes the 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 and finally to the persistent layer.
The program creates a second session and calls the update () method of the second session to persist these changes.
SaveOrUpdate (po) Does the following:
If the po object has been persisted in this session, saveOrUpdate will be executed in this session.
If savaOrUpdate (new po) has the same persistent identifier (identifier) with another po object associated with this session, an exception is thrown.
Org. hibernate. NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [org. itfuture. www. po. Xtyhb #5]
SaveOrUpdate: if the object does not have the identifier attribute, call save (). Otherwise, update () 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)
Main differences:
1. persist persists a transient instance, but the identifier (the attribute corresponding to the identifier primary key) is not guaranteed to be immediately entered into the persistence instance, enter identifiers may be postponed to flush.
2. save: generate a transient instance persistence identifier in a timely manner. It returns the identifier, so it will immediately execute SQL insert
V. Differences between saveOrUpdate, merge, and update
Compare update and merge
The role of update is mentioned above. Here we will talk about merge's
If an instance with the same persistent identifier (identifier) exists in the session, overwrite the existing persistent instance of the session with the object provided by the user.
(1) When we use update, an exception will be thrown after the execution is complete.
(2) When we use merge, we copy the attribute of the free-state po object A to the attribute of the persistent State po in the session, after the execution is complete, it turns out to be persistent or persistent, and the we provide is still free.
Vi. Differences between flush and update
These two differences are easy to understand.
Update is an object in the free or out-of-charge state (the session is closed but in the out-of-charge state). // updateSQL
Flush is the object in the persistent state of the operation.
By default, objects in a persistent State (including the set container) do not need to be updated, as long as you change the object value, wait for hibernate flush to automatically update or save it to the database. Hibernate flush occurs in the following situations:
1. Call some queries and manually flush (), close session, and close SessionFactory.
Get () is an object that changes the attributes of the object and closes the resource.
2. transaction commit (including flush)
7. Differences between lock and update
Update changes an object that has been changed to a persistent state.
Lock changes an unmanaged object to a persistent State (for a po object in the unmanaged state due to Session Shutdown (2 ), you cannot delete the po objects that are in the unmanaged status)
Change the content of a record. The two operations are different:
The update procedure is as follows:
(1) modify the unmanaged object after Attribute Modification-> call update
The lock operation procedure is:
(2) Call lock to change unmodified objects from the unmanaged state to the persistent state --> change the content of objects in the persistent state --> wait for flush or manually flush
VIII. Differences between clear and evcit
Clear the complete session cache
Evcit (obj) clears a persistent object from the session cache.

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.