Three statuses of Hibernate objects

Source: Internet
Author: User

Three statuses of Hibernate objects

We all know that there are three statuses of Data Objects in hibernate, So what are these three statuses? How are they converted? What are these statuses used? With this series of problems, let's take a look at the three statuses of hibernate.

The three statuses of hibernate are shown as follows:



Transient status (Transient): The object created by Java through the new method or the object obtained by the delete () method of the hosted and persistent State scheduling session. The value only exists in the memory and is not associated with the database. You can run the save ()/seavOrUpdate () method of the session to save the zero-point object in the memory to the database. In this way, the object is associated with the session, it is also associated with the database, which is a persistent object. If it remains in the memory for a long time and is not referenced by other objects, it will be reclaimed by Java's garbage collection mechanism to release the memory.

Pesistent): Saved by the Instantaneous State (the save () method for session scheduling) or directly queried from the session through Java (get () method for session scheduling) the result object obtained by the update () method of session scheduling in the managed status. Object information exists not only in the memory but also in the database, and the information in the memory is exactly the same as that in the database. I can run the transaction dispatch method to break the persistent state from the information in the database. At this time, it becomes the managed State, or we can call the delete () method of the session, delete it from the database, and then it becomes instantaneous.

Managed status (Detached): The object obtained after the method of transferring a transaction in the persistent state (after the transaction ends, it has nothing to do with the session. The object information exists in the memory and in the database, but the information in the database and the object information come out with the same Id. There is no other relationship between them. They can be different. After the update () method of the session is redeployed, the objects in the memory are synchronized to the data again and become persistent. Or you can call the delete () method to change the State from the database to the instantaneous state. If it remains in the memory for a long time and is not referenced by other objects, it will also be reclaimed by the Java memory recycle mechanism to release the memory.

Status \ description

Nature

Source

Location

Instantaneous status

Transient

It only exists in the memory and does not exist in the database.

1. new () directly by the program

2. Delete objects in persistent state

3. Delete objects in managed State

1. Save to persistent state

2. recycled by JVM

Persistent status

Pesistent

Exists in the memory and database, and their values are identical.

1. The program directly reads the database

2. Save the instantaneous status

3. managed status update

1. The deleted object changes to an instantaneous state.

2. Clear the session and change it to the managed State.

Managed status

Detached

It exists in memory and database, but the values in memory and database may be different except for primary keys.

1. Clear session in persistent state

1. Update the object to a persistent state.

2. The deleted object becomes persistent.

3. recycled by JVM

?

Note:

1. When an object is saved, it becomes persistent. If you modify the object, the object data will become dirty data in the memory, and then submit the object. HIbernate will synchronize the memory data to the database and dispatch the update method, even if no session is displayed. update () method.

2. When an object in the managed State is managed by the session again, the update method is directly called to synchronize the data and object data in the database, and the object is managed by the session again.

    Size: 30 KB view image attachments

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.