The three States of the hibernate object and the transition between them

Source: Internet
Author: User
Hibernate objects have three states: transient, persistent, and detached ). Objects in the persistent State are also known as Po (persistence object), and transient objects and unmanaged objects are also known as VO (value object ).
Instantaneous State
Use the new command to open up Java objects in the memory space,
Eg. person = new person ("XXX", "XX ");
If there is no variable to reference this object, it will be recycled by the Java Virtual Machine.
An instantaneous object exists in an isolated memory. It is a carrier carrying information and does not have any association with the database data. In hibernate, you can save () or saveorupdate () through the session () method: Associate the instantaneous object with the database and insert the corresponding data into the database. At this time, the instantaneous object is converted into a persistent object.

 

Persistent state
Objects in this state have corresponding records in the database and a persistent identifier. If the delete () method of Hibernate is used, the corresponding Persistent object becomes an instantaneous object. Because the corresponding data in the database has been deleted, the object is no longer associated with the database records.
After a session executes close (), clear (), and evict (), the persistent object becomes a pipe-dropping object, and the persistent object becomes a pipe-dropping object, although the object has a database recognition value, it is no longer managed by the Hibernate Persistence Layer.
Persistent objects have the following features:
1. associate with the session instance;
2. There are associated records in the database.

 

Detached
(1) When the Po objects obtained through the get or load method are all in the persistent State, but if the delete (PO) is executed (but the transaction cannot be executed), the PO state is in the detached state, (disassociation with the session). Changes to the Free State due to delete can be changed to the persistent State through Save or saveorupdate ().
(2) When the session is closed, the PO object of the persistent in the session cache is also changed to detached.
If the session is closed and becomes a free state, it can be changed to a persistent State through lock, save, and update.
A persistent instance can be detached by calling Delete.
The instances obtained through the get () or load () method are in a persistent state.
You can call lock () or replicate () to persist an instance in the unmanaged status. The pipe dropping object has the following features:
1. Essentially, it is the same as an instantaneous object. When no variable references it, JVM recycles it as appropriate;
2. A database record ID value is more than the instantaneous object.

 

The following figure shows the transition diagrams of three states: transient, persistent, and detached:


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.