The life cycle of persistent objects in hibernate (three states: Free State, persistent state, transition between free states)

Source: Internet
Author: User

Basic concepts of three states:

1, transient State (Transient): Also called Free State, exists only in memory, and there is no corresponding data in the database. Object created with new, it is not persisted, is not in session, the object in this state is called a temporary object;

2, Persistence State (persistent): associated with the session and has corresponding data in the database. has been persisted and added to the session cache. objects, such as those saved through hibernate statements. The object in this state is called a persistent object;

3, Free State (Detached): The persistent object is detached from the session object. Objects such as the session cache are emptied.
Feature: persistent, but not in session cache. The object in this state is called a free object;

x√

Temporary status

(Transient)

persisted state

(persistent)

Free status

(Detached)

is in session cache

x

x

< Is there a corresponding record in the p> database

x

 

 

 

 


The similarities and differences between free objects and temporary objects:

Both are not associated with the session, and the object properties and database may be inconsistent;

The free object has the persistence object to close the session and transforms, in the memory also has the object so at this time becomes the Free State;

Hibernate and SQL Relationships:

After the operation of hibernate method such as Save () and so on, and did not directly generate SQL statements to operate the database, but instead of these updates into the session, only the session cache to be updated, the underlying SQL statement ability to run, data stored in the database;

The following examples illustrate:
One, session.save (user) execution mechanism.
1. Add the user object to the cache, making it a persistent object;
2, use the mapping file to specify the identity of the generation ID;
3, run when the session clears the cache: generates an INSERT SQL statement at the bottom, and stores the object in the database;

Note: After you run Session.save (user), before the session cleans up the cache, if you change the User object property value, then finally the value of the database will be the last change of value, the process of the ID can not be changed;

Second, the session.delete (user) execution process.
Assuming that the user is a persisted object, the delete operation is run and the same underlying database runs with the following conditions: when the session cleans up the cache;
Suppose the user is a free object:
1, associating the user object with the session to make it a persistent object;
2, and then according to the user is persistent object process run;

Method of conversion between three states:

How does ① become a free State? The object becomes Free State by constructing method, and the persistent state and the Free State become Free State by the Delete method of the session .

How does ② become a persistent state? The object can be a persistent state directly by the load or get method of the session, and the Free State object can be persisted by means of the save,saveorupdate or persist method, and the Free State object can be Saveorupdate becomes a persistent state

How does ③ become a free State? the Free State can only be converted from a persistent state and implemented by the close or clear method.

Comparison of several conversion methods:

1.get and load

is loaded from the database into the data encapsulated as Java objects, so that the Java object from the Free State directly into a persistent state;

There are two differences: ①get the return object can be null, theload return value is always not NULL, the exception is thrown when it is not found ②get instant Insert is run, and load is running the insert when using this object

2.save,update and Saveorupdate

Save is a free State to a persistent state, and update is a free state into a persistent state , Saveorupdate can be said to be a synthesis of the two, it is run to infer the state of the object (mainly through the absence of the primary key inferred), if the Free State, then save, if the Free State, The update

3.save and persist

Both convert an object from a free state to a persistent state, but the return value is different:save returns the primary key value , and persist does not return

4,saveorupdate and Merge

Both the Free State or the Free State object is associated with the database, but the merge does not change the original state of the object

In addition, the clear and flush methods are also introduced. Clear is to turn all the objects in the session into a free state, a method by which the object is changed from a persistent state to a Free State (the second is to close the session), and the Flush method is used in order to make the update operation instantaneous (normally, Only the update operation is performed when the transaction is closed ).

The life cycle of persistent objects in hibernate (three states: Free State, persistent state, transition between free states)

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.