Three statuses of Hibernate objects

Source: Internet
Author: User

Three statuses of Hibernate objects

 

In hibernate, there are three object states: temporary state (Transient), Persistent state (Persistent), and Detached. All three state conversion methods are called through session, the characteristics of objects in three states are summarized as follows:

 

Temporary State: Not managed by session

There are no matching records in the database;

 

Persistent state: Included in session management

There are matched records in the database

When the attribute changes, it is automatically synchronized with the database when the cache is cleared (dirty data check;

 

Free State: Not managed by session

A matched record exists in the database.

 

Methods From temporary state to persistent state include save (), saveOrUpdate (), get (), and load ();

The methods from persistent state to temporary state include delete ();

Update (), saveOrUpdate (), lock ();

Persistent state to free state: session. close (), session. evict (), session. clear ().

 

 

Temporary State:

After a new object is created, the object is in the temporary state, that is, the object is only a memory area for storing temporary data. The data saved by this object has no relationship with the database, this object is converted to a persistent object only when the temporary object is associated with the database through Session save or SaveOrUpdate and the data is inserted or updated to the database.

If the new object is not referenced by a variable, it will be recycled by the Java Virtual Machine.

 

 

// Create an object Cat cat = new Cat (); cat. setName (miaomiao );... CatDaocd = new CatDao (); // persistence. The cat object is converted from the temporary state to the persistent State cd. save (cat );

 

Persistent state:

Instances of persistent objects have corresponding records in the database and have a persistence ID ). After the delete operation is performed on the Persistent object, the corresponding records in the database will be deleted, the persistence object and the database record will no longer have a corresponding relationship, and the persistence object will become a temporary state.

 

After the persistence object is modified, it will not be synchronized to the database immediately until the database transaction is committed.

 

 

// Create an object Cat cat = new Cat (); CatDaocd = new CatDao (); // match the object with the database record. The object is a persistent cat = cd. findById (1001); // modified the Persistent object but not synchronized to the database cat. setName (maomi); cd. save (cat); // synchronize the Persistent Object to the database Transaction. commit ();


 

 

CatDaocd = new CatDao (); // The persistent state changes to the temporary state cd. deleteByName (maomi );

 

Free State:

When the Session associated with the persistent object is Close, Clear, or evict, although the Persistent object has a persistent identifier and a value consistent with the corresponding records of the database, but the object is no longer in the Free State in the persistence layer management. The Free State object is similar to the temporary State object, but it also contains a persistent identifier (ID ). When the Free State object is re-associated to the session, it is converted to a persistent object through the update () and saveOrUpdate () methods.

 

In addition, if the object in the Free State is not referenced by the variable, it will also be recycled by the Java Virtual Machine.

 

Here we will summarize the three states of objects in Hibernate and make progress together ~

 

 

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.