A simple understanding of the concept of hibernate three states and their mutual transformation

Source: Internet
Author: User

This paper describes the concept of hibernate three states and their mutual transformation. There are three states in the life cycle of Java objects, and they are transformed into each other. They are temporary states, persistent states, and free states, respectively.

AD:WOT2015 Internet operations and developers Conference selling tickets

In hibernate there are three states, in-depth understanding of it in order to better understand the operating mechanism of hibernate, at first, not too much attention to these concepts, and later found that it is important. 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 life cycle, and they transform each other.

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

Hibernate three state two: persistent state (persistent): 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;

Hibernate three state three: Free State (Detached): The persistence 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;



The similarities and differences between free objects and temporary objects in hibernate three states:

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

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

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;

A simple understanding of the concept of hibernate three states and their mutual transformation

Related Article

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.