Hibernate three states

Source: Internet
Author: User

Hibernate entity Object state: Transient state ( Transient), persistent State (persistent), off-State (Detached)

A. Transient state:(value Object )/[VO]

When we generate an entity object from the Java New keyword, the entity object is in a free State, as follows:
Customer Customer=new Customer ("ZX", 27,images);
When the customer object is in a free State, why is the customer object in a Free state? This is because, at this point, the customer simply obtains a piece of memory through the JVM, and is not saved into the database through the Save () method of the Session object, so it is not included in Hibernate's cache management. This means that the customer object is now free to roam outside of hibernate cache management. So we can see that the most important feature of a free object is that there is no record in the database that corresponds to it.

B. Persistent State:(Persistence Object )/[PO]

The persisted object is the entity object that has been saved into the database, and the entity object is now in Hibernate cache management. This is any modification to the entity object that will be synchronized to the database when the cache is cleaned up. As shown below:
     Customer Customer=new Customer ("ZX", 27,images);
tx=session.begintransaction ();
Session.save (customer);
customer= (Customer) Session.load (Customer.class, "1");
Customer.setage (+);
tx.commit ();
At this point we do not show the call to the Session.update () method to save the update, but the modification of the entity object will be updated to the database synchronously, because the customer object is persisted after being saved into the database by the Save method. It is then loaded again through the Load method, which is still a persisted object, so it is still in the management of the hibernate cache, when the Tx.commit () method is executed, Hibernate automatically cleans up the cache and automatically synchronizes the persisted object's property changes to the database.

c. Off-state:   (Free State)

When a persistent object, separated from hibernate cache management, it is in a free state, the biggest difference between free objects and free objects is that the free object in the database may also have a corresponding record, but now this free object out of Hibernate cache management, The free object does not appear in the database with its corresponding data record. As follows:
     customer customer=new customer ("ZX", 27,images);
    tx= Session.begintransaction ();
Session.save (customer);
customer= (Customer) Session.load (Customer.class, "1");
Customer.setage (28);
Tx.commit ();
Session.close ();

When the session is closed, the customer object is not in Hibernate cache management, but there is also a data record in the database corresponding to the customer object, so the customer object is in a free state at this point.

conversions between three object states:
  Three kinds of object states that can be converted to each other, A free object can become a persisted object through the Session.save () method or the Session.saveorupdate () method, and a persisted object can be passed through the Session.flush () or the Session.evict () method, Moving out of the hibernate cache to a free object, a free object can be restored to a persisted object either by reloading or invoking the Session.update () method, or by calling the Session.delete () method to become a free object. and delete the corresponding data records in the database.
Sample program:

Configuration cfg = new configuration ();
Sessionfactory sf=cfg.configure (). Buildsessionfactory ();
Customer Customer=new Customer ("ZX", 27,images); The customer object is in Free State
Session session=sf.opensession ();
Transaction tx=session.begintransaction ();
Session.save (customer); The customer object is persisted after saving
Session.flush (); The customer object is in a free state after emptying the cache
Tx.commit ();
Session.close ();
Session session2=sf.opensession ();
Transaction tx2=session2.begintransaction ();
Session2.update (customer); The Free State of the customer object is converted to the persisted state again by calling the update () method
Session2.delete (customer); After the Delete () method is called, when the cache is emptied, the customer object is moved out of the cache and a delete transaction is generated in the database to delete the data records for the customer object
Tx.commit ();
Session.close ();

Hibernate 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.