Three states and transitions for hibernate objects

Source: Internet
Author: User

Three state transition graphs for Hibernate objects

        

three different states

instantaneous State (transient):

through New after an object is created, the object is not immediately persisted, and it does not participate in any association with the data in the database, at which point Java the state of the object is instantaneous. the session knows nothing about the instantaneous state of the object, and when the object is no longer referenced, her data is lost, and the object will be processed by the Java virtual machine in accordance with the garbage collection mechanism.

Persistent State (persistent):

when the object and Session Association, being Session when managed, it is in a persistent state. Objects that are in a persistent state have flags for the database. When did the object Establish a connection with the session?

(1) The interface through the session query, or get, or load method when loading objects from a database

(2) instantaneous state of an object, by invoking the save or saveorupdate method when the object is in a persistent state,session will continue to track and manage them if any changes are made to the internal state of the object, Hibernate Will choose the right time, such as when the transaction commits, to solidify the changes into the database.

Free State ( Detached):

Objects that are in a persistent state are in a free state after they are detached from the management of their session. After executing the evict,clear,close () method of the session , you can turn the persistent state into a free State

Session provides two methods (update,merge) that will be in the free State of the object, with a new session associated, the state of the object is re-persisted back to the persistent state.

State transitions:

1. Transient status transitions to persistent state

   After you save the object using the save() or saveorupdate() method of the session, it becomes persistent

Gets the object using the get or load method of the session object , and the state of the object is a persistent state.

2. permanent state to instantaneous state

After the delete() of the session is executed , it becomes a transient state

3. persistent state transitions to Free State

After the evict(),clear(), or close() method of The session is executed

4. Free State to persistent state

retrieves The session object, executes The update of the Session object , or Saveorupdate(), becomes persistent state

5. Free State to instantaneous state

After the delete() of the session is executed , the object has a free state to the instantaneous state.


objects that have changed before and after the state are called dirty objects

Dirty Check: when committing a transaction,hibernate examines the persistent state object in the session to see if the data has changed

Objective: ensure that data objects in memory are consistent with data in database tables

Refresh cache mechanism:

1. When executing the commit method of a transaction, you can flush the cache

2. Execute the session.flush () method

Three states and transitions for hibernate objects

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.