Proficient in the state of Java objects in Hibernate--hibernate

Source: Internet
Author: User

Hibernate requires the following States in his life cycle for Java objects to be persisted:
1, temporary State (transient): Just created with the new statement, has not been persisted, not in the session cache. A Java object that is in a temporary state is called a temporary object.
2, persistent State (persistent): Has been persisted, added to the session, in the persisted state Java object is called persisted object.
3, Free State (detached): Has been persisted, but not in the session.
Let's take a look at the Customer object state transition process

Characteristics of temporary objects
1, not in the session cache, is not associated with any one session instance
2, there is no corresponding record in the database
The object enters the temporary object state in the following situations
1. When the object that was just created through the new statement
2. The Delete method of the session can convert a persisted object or a free object into a temporary object. For a free object, the Delete method deletes the record corresponding to him from the database; For persisted objects, the Delete method deletes the corresponding record from the database, and for the persisted object, the Delete method deletes the corresponding record from the database and removes it from the session.
characteristics of persisted objects
1. In the cache of a session instance, the persisted object is always associated with a session instance
2. Persistence objects and records in the database correspond
3, the session in the cleanup cache, according to the persistence of the properties of the object changes, to synchronize the update database, the session of many methods can trigger the Java object into the persistent state
4, session of the Save method to convert the temporary object to a persistent object
5. The object returned by the load or get method of the session is always in the persisted state
6, the session of the Find method returns the list collection is stored in the persisted object
7, session update, Saveorupdate, lock method to transform the free object into a persistent object
8. When a persisted object is associated with a temporary object, in the case of a cascade save, the session will also convert the temporary object to a persisted object when it cleans up the cache.
Hibernate guarantees that in the cache of the same session instance, each record in the data table only corresponds to a unique persisted object. As follows:

Session Session1 = sessionfactory. Opensession();Session Session2 = sessionfactory. Opensession();Transaction tx1 = Session1. BeginTransaction();Transaction TX2 = Session2. BeginTransaction();Customer A = (customer) Session1. Load(Customer. Class, New Long (1));Customer B = (customer) Session1. Load(Customer. Class, New Long (1));Customer C = (customer) Session2. Load(Customer. Class, New Long (1));System. out. println(A = = b);//TrueSystem. out. println(A = = c);//Fals

Characteristics of Free objects
1, no longer in the session cache, not by the session Association
2, the free object is changed by the persistent object, so there may be in the database with his corresponding records
The following method of the session turns the persisted object into a free State
1. When the session's Close method is called, the session's cache is emptied
2. The evict method of the session can remove a persisted object from the cache and make him free. When a large number of persisted objects are saved in the session cache, it consumes a lot of memory space, and in order to improve performance, consider calling the evict method to remove some persisted objects from the cache. However, most cases are not recommended, but the depth of the object graph is controlled by the query language or explicit navigation.

Proficient in the state of Java objects in Hibernate--hibernate

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.