Three states of objects in Hibernate, and conversion methods between three states

Source: Internet
Author: User

In hibernate there are three states, the deep understanding of it, to better understand the operation mechanism of hibernate, at first, not too much attention to these concepts, and later asked by others, said is the interview problem, only to find it is important. For Java objects that need to be persisted, there are three states in its life cycle, and they transform each other.

hibernate one of three states: temporary state (Transient): Also known as instantaneous state , when an object has just been created, such as people people = New people (), it is not persisted, is not in the session cache, this state of the object is called temporary object;

hibernate three states: Persistence State (persistent): the Save () method has been executed, has been persisted, added to the session cache, and has a corresponding record in the database. and has a persistent identity. An object in this state is called a persistent object.

At this point, if you use Hibernate's 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 records. Persistent objects have the following characteristics: 1. Associated with the session instance; 2. There is a record associated with it in the database.

When a session executes close () or clear (), evict (), the persisted object becomes the following riser object, at which point the persisted object becomes a de-tube object, while the object has a database recognition value, but it is not under the management of Hibernate persistence layer. That is, its record in the session cache has been deleted.

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

Three states of objects in Hibernate, and conversion methods between three states

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.