Understanding the concept and transformation of three states of session in Hibernate

Source: Internet
Author: User

In the hibernate, there are three states, in-depth understanding of it, in order to better understand the operating mechanism of hibernate, just started to pay little attention to these concepts, and later found that it is important. Have a better understanding of the relationship between HIBERNATE,JVM and SQL. For Java objects that need to be persisted, there are three states in its lifecycle, and they transform each other.

Hibernate one of three states: temporary state (Transient): An object created with new, it is not persisted, is not in session, and the object in this state is called a temporary object;

Hibernate three state bis: Persistent State (persistent): Has been persisted and added to the session cache. such as objects saved through the Hibernate statement. Objects in this state are called persistent objects;

Hibernate three state three: Free State (Detached): The persisted object is detached from the session object. Objects such as the session cache being emptied. Feature: Has persisted, but is not in the session cache. The object in this state is called the free object;



the similarities and differences of free and temporary objects in three hibernate states:

Neither will be associated with the session, and the object properties and database may not be consistent;

The free object is transformed by the persistent object closing session and the object in memory, so it becomes a free state at this time;

the relationship between Hibernate and sql:

In the operation of the Hibernate method such as Save () and so on, and did not directly generate SQL statements to operate the database, but the update into the session, only the session cache to be updated, the underlying SQL statements can be executed, data into the database;

The following examples illustrate:

First, Session.save (user) operating mechanism.
1, the user object is added into the cache to make it a persistent object;
2, select the identification generated ID specified by the mapping file;
3, in the Session cleanup cache execution: Generate an Insert SQL statement at the bottom, the object into the database;

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

Second, Session.delete (user) running process.
If the user is a persisted object, the deletion is performed, as is the case with the underlying database: when the session cleans up the cache;
If user is a free object:
1, associating the user object with the session, making it a persistent object;
2, and then executes according to the process of the user being the persisted object;

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.