06Hibernate entity class life cycle

Source: Internet
Author: User

Preface:The session interface is the primary interface for hibernate to provide an operational database to the application, and it provides a basic method for adding and removing additions and deletions, and the session has a cache that is Hibernate's first-level cache. Standing in the persistence layer, hibernate divides the entity class into 4 states: persistent state, temporary state, Free State, and delete state. This article will introduce the details of hibernate's mechanisms and how to use them. Session cache for 1.Hibernate (1) function of Session cacheUsing the session cache has three main functions:
      1. Reduce the frequency of access to the database, when the session's get () method tries to load an object from the database, the session first determines whether the object exists in the cache, and if it does not need to be retrieved from the database, the object is fetched directly from the cache.
      2. When a cyclic relationship exists between persisted objects in the cache, the session guarantees that there will be no dead loops to access the object graph, and the JVM stack overflow exception caused by the loop.
      3. session ensures that the relevant records in the database are kept in sync with the corresponding objects in the cache. The session will be automatically dirty when the cache is cleaned up, and if the objects in the session cache are found to be inconsistent with the corresponding records in the database, the database will be updated synchronously based on the latest properties of the object.
(2) Hibernate's dirty check and cleanup session cache principleWhen the properties of an object in the session cache change, the session does not immediately clean up the cache and executes the associated SQL UPDATE statement, but cleans the cache at a specific point in time, allowing the session to merge several related SQL statements into one SQL statement. To reduce the number of times the database is accessed.
When the session cleans up the cache, it executes the SQL in the following order: INSERT, UPDATE, and then delete. Specific as follows:
      1. Executes all INSERT statements inserted into the entity class according to the order in which the application calls the Session.save () method.
      2. Executes all UPDATE statements that are updated on the entity class.
      3. Executes all DELETE statements that have been deleted from the entity class collection.
      4. Executes all SQL statements that delete, update, or insert an entity class collection.
      5. Executes all INSERT statements that are inserted into the entity class collection.
      6. Executes all DELETE statements that are deleted from the entity class according to the order in which the application calls the Session.delete () method.
By default, the session cleans up the cache in the following scenarios:
      1. When the application calls Org.hibernate.Transaction's commit () method, the commit () method cleans up the cache before committing the transaction.
      2. When an application calls Hibernate to execute a query, if the object in the cache is inconsistent with the database (that is, the object in the cache has changed), hibernate cleans the cache and executes the query to ensure that the query gets the correct data.
      3. When an application explicitly invokes the flush () method of the session.
Note:The exception to the session cleanup cache is that if the object uses native to generate the OID, the INSERT statement is immediately executed when the session's Save () method is called to ensure that the OID of the object is immediately obtained from the database.
If you do not want the session to clean up the cache at the above default point in time, you can also call Session.setflushmode (Flushmode)method to set the mode in which the cache is cleaned. The specific pattern is as follows:         clearing the cached schema query commit () method flush () method        Flushmode.auto Cleanup cleanup        flushmode.commit do not clean up and clean up        flushmode.never do not clean up clean up Note:The session's commit () method differs from the flush () method, which cleans up the cache, but commit () commits the transaction. 2. Status of entity classes in hibernate containers (1) three states of Hibernate entity classesStanding in the perspective of persistence, a Java entity class can have a life cycle of four states, as follows:
      1. Temporary state (transient): An entity class that has just been created with the new statement and is not persisted and is not in the session's cache.
      2. Persistence State (persistent): An entity class that has been persisted and is in the session cache.
      3. Delete State (removed): The entity class that is not in the session cache and that the session is scheduled to remove from the database.
      4. Free State (detached): An entity class that has been persisted but is no longer in the session's cache.
The state of an entity class can vary, and its process is as follows:           (2) Characteristics of entity class life cycle states characteristics of the temporary state entity class:
      1. In the case of a proxy primary key, the OID is null.
      2. is not in the session cache.
      3. There are no corresponding records in the database.
Persistence State entity classes are characterized by:
      1. The OID is not NULL.
      2. In the session cache
      3. There is a corresponding record in the database.
features of the Delete state entity class:
      1. The OID is not NULL.
      2. is not in the session cache.
      3. The session plans to remove it from the database.
      4. When the session cleans up the cache, it executes the corresponding DELETE statement.
characteristics of the Free State entity class:
      1. The OID is not NULL.
      2. is not in the session cache.
      3. There is a corresponding record in the database.
The detailed usage of the 3.Session interface The session interface is the most important interface that hibernate provides to the application to manipulate the database, and this section explains its main use methods. (1) The process of creating a session


(2) the Save () and persist () methods of the session



(3) The Get () and load () methods of the session



(4) the update () and Saveorupdate () methods of the session


(5) The merge () method of the session


(6) Delete () method of Session



(7) The Replicate () method of the session


4.Hibernate Cascade Operation entity Class






--------------------------------------------------------------------------------------------------------------- ----------------

From for notes (Wiz)

06Hibernate entity class life cycle

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.