The three states of objects in Hibernate and the transformation methods between three states __java

Source: Internet
Author: User

In hibernate, there are three states, in-depth understanding of it, in order to better understand the operating mechanism of hibernate, just started not to pay attention to these concepts, and then was asked by others, said the interview question, just found it is important. 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): Also called instantaneous state , when an object is just created, such as people people = New people (), it is not persisted, is not in the session cache, the object of this state is called temporary object;

hibernate three State bis: Persistent State (persistent): the Save () method of the session has been executed, has been persisted, added to the session cache, and has a corresponding record in the database. and has a persistent identity. Objects in this state are called persistent objects.

At this point, if you use the hibernate Delete () method, the corresponding persistent object becomes the instantaneous object above, because the corresponding data in the database has been deleted and the object is no longer associated with the database's records. Persistent objects have the following characteristics: 1. Associated with the session instance; 2. There are records associated with them in the database.

When a session executes close () or clear (), evict (), the persistent object becomes the one that is to be spoken, and the persisted object becomes a Hibernate object, which, although it has a database-aware value, is no longer under the management of the persistence layer. That is, its record in the session cache has been deleted.

hibernate three state of three: Free State (Detached): Also called the Detachment State , the persistent object detached from the session object. If the session is closed, the session caches the objects that are emptied. Feature: Has persisted, but is not in the session cache. An object in this state is called a free object, and a free State by closing the session can be changed to a persistent state by lock, save, and update.

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.