Object status in Hibernate and reasons for automatic update

Source: Internet
Author: User

Hibernate objects have three states:Transient, Persistent, and Detached). So that you can better understand the running mechanism of hibernate.

I. Instantaneous status(Transient)

The new command is used to open up a java object in the memory space. It is not persistent and is not in the Session. The object in this state is called an instantaneous object. Example:

Person person = new Person ("Han Meimei", "female ");

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.

Ii. Persistence status(Persistent)

It has been persisted and added to the Session cache.

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 decentralized object. At this time, although the object has a database recognition value, however, it is no longer under the management of the HIbernate Persistence Layer.

Persistent objects have the following features:

1. associate with the session instance;

2. There are associated records in the database.

Iii. Free Status(Detached)

When the session associated with a persistent object is closed, the persistent object is converted to a unmanaged object. When the unmanaged object is re-associated to the session, it is converted into a persistent object again.

The unmanaged object has the recognition value of the database. It can be converted to a persistent object through update (), saveOrUpdate (), and other methods.

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.

 

Next, many of my friends asked me that he didn't see the updated code, but the data in the database was updated. This problem was caused by Hibernate.

In a transaction, if a new value is set for a persistence object, Hibernate will compare which persistence objects have changed when the transaction is committed. If yes, it is automatically updated to the database. (Note that if you perform operations outside of the transaction, Hibernate will not update it for you)

Someone on the Internet said that the free state object will be automatically updated if the property is changed, so I will not test it. You can try it.

To avoid this problem:

1. Operate this object outside the transaction;

2. Clone an object.

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.