The state of the Java object at the Hibernate persistence layer __java

Source: Internet
Author: User

Original address codergo:http://leekai.me/?p=219 introduction

For the JVM, the state of the Java object is simple, a new object created by new, a garbage object that is not referenced by any object, and for garbage objects, the JVM reclaims it at the right time. For hibernate, a Java object can have four states: a temporary state (transient): Just created with the new statement, not persisted, and not yet in the session cache. A Java object that is in a temporary state is called a temporary object. Persistent State (persistent): Has been persisted and added to the session's cache. A Java object that is in a persistent state is called a persisted object. Delete status (removed): no longer in the session's cache, and session is scheduled to be removed from the database. A Java object that is in the deleted state is called a Delete object. Free State (detached): Has been persisted, but is no longer in the session's cache. A Java object in the Free State is called a free object.
One, Java object state transitions

Figure 1 State transition diagram of Java objects in hibernate
second, the object state detailed 1. The temporary state detailed explanation

Temporary state There is a feature: in the case of a surrogate primary key, the OID is usually null. is not in the session's cache, and is associated with any one of the session instances. There is no corresponding record in the database

In the following case, the Java object enters a temporary state: A Java object is created using the new statement. In the Hibenrate configuration file, the Hibernate.user_identifier_rollback property is true, and the session's delete () allows a persisted object or free object to be converted to a temporary object. 2. Characteristics of Persistent Objects

The persisted object has the following characteristics: OID is not null. is in the session cache, that is, the persisted object is always associated with the session instance. The persisted object corresponds to a corresponding record in the database. When the session cleans up the cache, the database is updated synchronously, depending on the properties of the persisted object. The session's Save () converts the temporary object to a persisted object. The session's load () or get () method returns an object that is persisted. The object returned by Query.list () is a persisted object. The session's update (), Saveorupdate () and Lock () methods enable the free object to be transformed into a persisted object. When a persisted object associates a temporary object, allowing cascading saves, the session transitions the temporary state to a persisted object when it cleans up the cache. 3. Features of Deleted objects

The deleted object has the following characteristics: OID is not null. Removed from the cache of a session instance. The deleted object corresponds to the related record in the database. The session schedule deletes it from the database. When the session cleans up the cache, it executes the SQL DELETE statement and deletes the corresponding record. In general, applications should no longer use deleted objects.

The Java object enters the deletion state when the default value of the Hibernate.use_identifier_rollback property in the Hibernate configuration file is False, Session.delete () Converts the persisted object and the free object to the deleted object. When a persisted object A is associated with a persistent object B, allowing cascading deletes, the session deletes the persisted object B,a and B all go to the deletion state when it removes the persisted object A. 4. Characteristics of Free Objects

The free object has the following characteristics: The OID is not null and is no longer in the session cache and is not associated with the session. The free object is transformed by the persisted object, so there may be records in the database.

The same points and differences between the free and temporary states:

same: None is associated with the session, Hibernate does not guarantee that the object is synchronized with the database record.
difference: The free object is transformed by the persisted object, and there may be records in the database, the OID is not NULL, the temporary object is not, and the OID is null.

The free object and the deleted object are the same and different:

same: are not in the session's cache, and the database may have corresponding records.
different: The free object is completely divorced from the session, the deleted object is deleted from the database by the session schedule, and the SQL DELETE statement is executed, and the record is deleted from the database, when it clears the cache.

The following method of session causes the persisted object to be transformed into a free object: When the Close () method of the session is invoked, the session cache is emptied and all persisted objects in the cache become free objects. If they are not referenced by the Java application, they are then reclaimed by the garbage collector later. The Sesision.evict () method clears a persisted object from the cache and changes it to a free object. Storing too many persisted objects in the session cache consumes memory. Session.clear () Clears all persisted objects in the cache so that they become free objects. Summary

--This article is "Proficient in Hibernate" 1 reading notes

The state and state transitions of Java objects in the Li Qingchu session are critical, and in your application you should be aware of the state in which each Java object is hibernate in order to avoid/exclude certain errors. attached:

1. Proficient in Hibernate:java object persistence technology detailed.

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.