Java Learning (16) __hibernate three state distinctions, and the use of save,update,saveorupdate,merge, etc.

Source: Internet
Author: User
Tags flush

The Hibernate objects have 3 states, namely instantaneous State (Transient), persistent State (persistent), and de-tube State (Detached). Objects that are in a persistent state are also called PO (Persistence object), and the instantaneous objects and the off-pipe objects are also called VO (Value object).
Instantaneous state
A Java object that opens up memory space by the new command,

eg. person person = new person ("xxx", "xx");

If no variables are referenced to the object, it is reclaimed by the Java virtual machine.

The instantaneous object exists in the memory, it is carries the information the carrier, does not have any correlation with the database data, in the hibernate, May through the session's save () or the Saveorupdate () method to associate the instantaneous object with the database, and inserts the data corresponding to the database , the instantaneous object is transformed into a persisted object.

Persistent state
An object in this state has a corresponding record in the database and has a persistent identity. If you use the hibernate Delete () method, the corresponding persistent object becomes an instantaneous object, because the corresponding data in the database has been deleted and the object is no longer associated with the database's records.

When a session executes close () or clear (), evict (), the persistent object becomes a pipe-off object, and the persisted object becomes a pipe-off object, which, while having a database-aware value, is no longer under the management of the Hibernate persistence layer.

Persistent objects have the following characteristics:

1. Associate with session instance;

2. There are records associated with them in the database.

Off-Pipe State
When the session associated with a persistent object is closed, the persisted object is converted to a pipe-off object. When the off-pipe object is again connected to the session, it is transformed into a persistent object again.

The Saveorupdate object has the identification value of the database, and can be transformed into a persistent object by means of update () and the method of the ().

The removal of the pipe object has the following characteristics:

1. Essentially the same as the instantaneous object, the JVM will recycle it at the appropriate time without any variables referencing it;

2. More than the instantaneous object a database record identity value.

Hibernate of various ways of saving (Save,persist,update,saveorupdte,merge,flush,lock) and three states of objects
Hibernate save
Hibernate there are so many ways to save objects, and there are a lot of differences between them.
First, preliminary knowledge
for Hibernate, its object has three states, transient, persistent, detached
below is a common translation method:
Transient: transient or Free State
(new DEPTPO (1, "Administrative department", 20, "administrative related"), the instance of the PO is not associated with the session, the instance of the PO is in transient
persistent: persistent state
(and the instance of the PO in the database that you want to insinuate, Its state is persistent, objects obtained through get and load are all persistent)
detached: off-pipe state or Free State
(1) When PO objects obtained by GET or Load method are in persistent, However, if the Delete (PO) is executed (but the transaction cannot be performed), the PO State is detached, (representing a session detachment), and the delete becomes a free state by using Save or saveorupdate () to become a persistent state
(2) When the session is closed, the persistent PO object in the session cache also becomes detached
becomes free by closing the session by lock, save, update to persistent state
A persistent state instance can become a state of being out of control by calling Delete (). An instance of the
obtained through the Get () or load () method is persisted. An instance of the
off-pipe state can be persisted by invoking lock () or replicate ().

Save () and persist () will raise the SQL Insert,delete () to raise the Sqldelete.
Update () or merge () raises SQL update. Modifications to a persistent (persistent) instance are detected when a refresh is committed, and it also causes SQL UPDATE.
Saveorupdate () or replicate () can cause sqlinsert or update
Two, save and update differences
The reason to put the pair first is because the pair is the most common.
The role of Save is to save a new object
Update is an object or a Free state object (must correspond to a record) to the database

Iii. Update and Saveorupdate differences
This is a relatively good understanding, as the name suggests, Saveorupdate basically is the synthesis of save and update, and update is only update; quote a passage from Hibernate reference to explain their use and differences.
Usually 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
Some changes have occurred to the object
The object is returned to the business logic layer and finally to the persistence layer
The program creates a second session the update () method that calls the second session persists these changes

Saveorupdate (PO) do the following:
If the PO object has persisted in this session, execute 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, throws an exception
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 (), or the update () object

Iv. persist and save differences
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 document.
Here is a clear distinction. (Can follow SRC look, although the implementation steps are similar, but there are subtle differences)
Main content difference:
1,persist a transient instance is persisted, but the "not guaranteed" identifier (the attribute corresponding to the identifier primary key) is immediately filled into the persisted instance, and the filling of the identifier may be deferred to the flush.

2,save, which takes a transient instance to persist the identifier, produces it in time, and it returns an identifier, so it immediately executes the SQL insert

Five, Saveorupdate,merge, and update differences
Compare the role of update and merge
Update it says, here's the merge
if the same persistence identity exists in the session ( identifier), using the object given by the user to overwrite the existing persistent instance of the session
(1) When we use update, the execution completes, throws an exception
(2) But when we use the merge, Copy the property of the Po object A that handles the free State to the properties of the persistent PO in the session, whether the execution is persistent or persistent, and we provide a free State

The difference between

Six, flush, and update
is a good understanding of the
Update operation is an object that is in a free State or in a//updatesql state (which is out of control because of session shutdown)
The flush is the object of the operation in a persistent state.
By default, changes to a persisted state object (including the set container) do not need to be updated, as long as you change the value of the object and wait for the Hibernate flush to automatically update or save to the database. The Hibernate flush occurs in the following situations:
1, calling certain queries and manual flush (), session closure, sessionfactory closing, combining
get () An object, changing the object's properties to turn the resource off.
2,transaction commit (including flush) seven, lock, and update differences
Update is to make a persistent state of an object that has already been changed.
Lock is a persistent state of an object that has not changed the state of being out of control (for a PO object (2) that is in a state of being off due to a session shutdown, you cannot change the contents of one record for a PO object that is in the out of control because of delete, two different actions:
Update steps are:
(1) The modification of the-> object after the property has changed. The procedure for calling update
Lock is:
(2) Invoking lock to change an unmodified object from a state to a persistent state--> Change the contents of an object that is persisted--> wait for flush or manually flush
Eight, clear, and evcit differences
Clean clears the session cache
Evcit (obj) Clears a persisted object from the session's 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.