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

Source: Internet
Author: User
Tags commit flush

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;

Create a Transient object
  user user = new user ();
  User.setname (userName);
  User.setpassword ("test");
  User is still a transient 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;

Create a Transient object
  user user = new user ();
  User.setname (userName);
  User.setpassword ("test");
  User is still a transient object

session session = Sessionfactory.opensession ();
Tansaction tx = Session.begintansaction ();
At this point the user is still a transient object
session.save (user);
At this point the user has become persistent state
tx.commit ();

Tansaction tx2 = Session.begintansaction ();
User.setpassword ("PDW");
Tx2.commit ();
Although the Save () method of the session is not used to save the user object, USR is persistent too,
//So any modifications made to the user object persist to the database   ---------Persistent State Auto-update
//Then the password in the database becomes the PDW

session.close ();

Another method directly obtains the persistent state object:

Session session = Sessionfactory.opensession ();
Tansaction tx = Session.begintansaction ();
User user = (user) session.load (user.class, "key"),   ------equivalent to HQL  read from DB is of course persistent state
//user object is already a persistent object
Tx.commit ();
Session.close ();

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;

Create a Transient object
  user user = new user ();
  User.setname (userName);
  User.setpassword ("test");
  User is still a transient object

session session = Sessionfactory.opensession ();
Tansaction tx = Session.begintansaction ();
At this point the user is still a transient object
session.save (user);
At this point the user has become persistent state
tx.commit ();
Session.close ();      Clear ()   evict ()  3 variable free states
The user object has become Free State

session Close, the object becomes a free state.


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 to 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 to 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 a persistent state by update,saveorupdate.

③ how to 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: ①get returns an object that can return a value of null,load is always not NULL, throws an exception when it is not found ②get instant execution of an INSERT, while load executes the insert when using this object

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 executes the first to judge the state of the object (mainly by having the primary key to judge), if the Free State, then save, if it is Free State, then 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).

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.