The State and transformation of Hibernate entity objects

Source: Internet
Author: User

Status and transformation of entity objects:   with the above about Hibernate The knowledge of caching, we can then introduce the state of the entity object is very easy to understand. A: Free State object: when we pass Java of the New keyword to generate an entity object, the entity object is in a free State, as follows: Customer Customer=new Customer ("ZX", 27,images); then Customer the object is in a free State, why do you say Customer The object is in a free state. This is because, at this time Customer just through JVM Got a piece of memory space and didn't pass Session object's Save () method is saved into the database, so it is not included in the Hibernate In cache management, which means Customer objects are now free to roam Hibernate outside of cache management. So we can see that the biggest feature of the free object is that there is no record of it in the database. B: Persistent Objects : The persisted object is the entity object that has been saved into the database, and the entity object is now 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 (a); tx.commit (); At this time we did not show the call session.update () method to save the update, but modifications to the entity object are synchronized to the database, because at this point Customer object is passed Save method is persisted to the database and then passed through the Load method to load it again, it is still a persisted object, so it is still Hibernate Cache management, when the execution Tx.commit () method, Hibernate automatically cleans up the cache and automatically synchronizes the property changes of the persisted objects to the database. C: Free objects: when a persistent object, detached from the open Hibernate Cache management, it is in the Free State, free objects and free objects, the biggest difference is that the free object in the database may still have a corresponding record, but now this free object from the Hibernate cache management, and the free object does not appear in the database with its corresponding data record. 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 (a); tx.commit (); session.close (); when Session after closing, Customer object is not in Hibernate Cache management, but there is also a Customer object to the data record, so at this point Customer object is in a free state. D: transformation between three object states:   these three object states can be converted to each other, and a free object can be Session.save () method or session.saveorupdate () method into a persisted object, a persisted object can be passed through the Session.flush () or session.evict () method, remove Hibernate the cache is then converted to a free object, and a free object can be loaded or called session.update () method to revert to the persisted object again, or by calling the Session.delete () method becomes a free object and deletes the corresponding data record in the database.   Sample programs:   Configuration cfg = new Configuration (); sessionfactory sf=cfg.Configure (). Buildsessionfactory (); Customer Customer=new Customer ("ZX", 27,images); Customer object is in a Free State Session Session=sf.opensession ();   Transaction tx=session.begintransaction (); Session.save (customer);/ after saving Customer object in persistent state Session.flush ()// after emptying the cache Customer object is in a Free State tx.commit (); session.close ();   Session Session2=sf.opensession (); Transaction tx2=session2.begintransaction (); session2.update (customer);/ by calling Update () method will free the state of the Customer object, converted again to a persisted state Session2.delete (customer);/ Call Delete () method, when the cache is emptied, the Customer object is moved out of the cache and generated in the database Delete transactions, to remove Customer object corresponding to the data record tx.commit (); session.close ();

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.