Hibernate three states of distinction, and the use of save,update,saveorupdate,merge, etc.

Source: Internet
Author: User

There are 3 types of hibernate objects: transient (Transient), persistent (persistent), and off-state (Detached). Objects that are in persistent state are also known as PO (persistence object), and instantaneous and De-tube objects are also known as 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 the object is not referenced by a variable, it will be reclaimed by the Java virtual MACHINE.

Instantaneous objects exist in memory, it is the carrier that carries the information, does not have any relation with the database data, in hibernate, the instantaneous object can be associated with the database through the Session's save () or Saveorupdate () method, and the data corresponding to the inserted database , the instantaneous object is transformed into a persisted object.

Persistent state
The object in that state has a corresponding record in the database and has a persistent identity. In the case of Hibernate's delete () method, The corresponding persistent object becomes instantaneous, because the corresponding data in the database has been deleted, and the object is no longer associated with the Database's Record.

When a session executes close () or clear (), evict (), the persisted object becomes a de-tube object, at which point the persisted object becomes a de-tube object, while the object has a database recognition value, but it is not under the management of Hibernate persistence Layer.

Persistent objects have the following characteristics:

1. Associate with the session instance;

2. There is a record associated with it in the Database.

Off-pipe State
When the session associated with a persisted object is closed, the persisted object is turned into a de-tube object. When the Off-tube object is re-associated to the session, it is again transformed into a persistent object.

The De-tube object has the recognition value of the database and can be transformed into a persistent object by means of update (), saveorupdate () and so On.

The Off-pipe object has the following characteristics:

1. Essentially the same as an instantaneous object, when no variable references it, the JVM will recycle it at the appropriate time;

2. More than the instantaneous object a database record identification Value.

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
for hibernate, The object has three states, transient, persistent, detached
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 throw a SQL insert, Delete () throws sqldelete,
and update () or merge () raises SQL Update. Modifications to a persisted (persistent) instance are detected when the commit is refreshed, and it also causes SQL UPDATE.
saveorupdate () or replicate () raises sqlinsert or update

The reason for putting this pair first is because the pair is the most Common.
save is to save a new object
update is to update a De-state object or a Free state object (must correspond to a record) to the database

three, update and saveorupdate differences
Span style= "font-size:16px; Color: #0000ff "> This is a good understanding, as the name implies, Saveorupdate basically is the synthesis of Save and update, and update is just update; reference hibernate A passage in reference to explain their use and differences
Usually the following scenario uses the update () or saveorupdate ():  
Program loads the object in the first session, then closes the session  
The object is passed to the presentation layer  

The object is returned to the business logic layer eventually to the persistence layer  
program creates a second session calls the update () method of the second session to persist these changes

saveorupdate (po) do the following:  
If the PO object is already persisted in this session, do not do anything in this session saveorupdate  
If savaorupdate (new Po) has the same persistent identity as another PO object associated with this session ( identifier), 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 () on it, or update () this 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 is the

(1) When we use update, we throw an exception   when the execution is done,
(2) But when we use the merge, Copy the properties of the Po object A that handles the free State to the properties of the PO that is in the persistent state in the session, whether it was persisted or persisted after execution, and whether we provided a or a free State

vi, flush and update differences
The difference between the two is good understanding
update operation is in a free State or a de- An object that is in A//updatesql state due to the closing of a session)
and flush is an object that operates in a persisted State.
by default, changes to a persisted state object (including the set Container) are not required for update, as long as you change the value of the object, Wait for hibernate flush to be automatically updated or saved to the Database. Hibernate flush occurs in the following cases:
1, invoking some queries and manual flush (), Session closed, Sessionfactory closed combined with  
get () an object, Change the properties of the object to close the Resource.
2,transaction commit (contains flush)

seven, lock and update difference
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 Off-pipe state due to the closing of the session, not for a PO object that is in a de-state due to Delete)
for changing the contents of one record, the operation of the two is different:
update the steps are:
(1) modification of the Off-tube object after the property change, call Update
(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
eight, clear and evcit the difference
clear Complete purge session cache
evcit" (obj) empties a persisted object from the Session's cache.

Hibernate three status distinctions, and use of save,update,saveorupdate,merge, etc.

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.