Analysis of three states of Po objects in hibernate

Source: Internet
Author: User

Hibernate status
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

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

(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.

Session. Lock (xtyhb, lockmode. None); // indicates to directly go to the cache to find the object that becomes a persistent state.

Session. lock (xtyhb, lockmode. read); // read the database's record ID through the ID to check whether the record exists. If there is a record, find the object that becomes persistent in the cache.

Post source DIY tribe (http://bbs.diybl.com) detailed source reference: http://bbs.diybl.com/dview10t5408.html

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.