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, temporary 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)

Persistent state

(persistent)

Free State

(Detached)

is in the session cache

X

X

Is there a corresponding record in the 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 can be executed, the data into the database;

The following examples illustrate:
One, Session.save (user) operating mechanism.
1, the user object is added to the cache, so that it becomes a persistent object;
2, use the mapping file to specify the identity of the generation ID;
3, when the session cleanup cache execution: At the bottom of the generation of an INSERT SQL statement, the object stored in the database;

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

Two, Session.delete (user) running process.
If the user is a persisted object, then the delete operation is performed, and the execution condition of the same underlying database is: when the session cleans up the cache;
If user is a free object:
1, associating the user object with the session to make it a persistent object;
2, and then follow the process of user is persisted object;

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 directly persisted 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 is implemented by the close or clear method.

Comparison of several conversion methods:

1.get and load

are loaded from the database into Java objects, so that the Java objects from the Free State directly into the persistent state;

But there are two differences: The ①get return object can be null, theload return value is always not NULL, it throws an exception when it is not found ②get instant execution of the INSERT, while load is using this object to execute the insert

2.save,update and Saveorupdate

Save is the Free State into a persistent state, and update is the Free State into a persistent state , Saveorupdate can be said to be the synthesis of the two, it is executed to determine the state of the object (mainly through the primary key to judge), if the Free State, then save, if it is 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 way for the object to become free from the persistent state (the other is to close the session), and flush the method in order for the update operation to take place immediately (under normal circumstances, The update operation only occurs 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.