Three states of Hibernate objects

Source: Internet
Author: User

three states of Hibernate Java objects

I. Brief description

  Most of the ideas and content of this blog post csnd on the name of FG2006 , the great god of the article, the connection address is: http://blog.csdn.net/fg2006/article/details/6436517. I apologize and deeply thank you for that. If you read in the course of the discovery of any mistakes or errors, please timely, I greatly appreciate,

There are three states for objects in Hibernate: Temporary (Transient), persistent (persistent), Free State (Detached).

  Temporary state : just created with the new statement, it is not persisted and is not in the session's cache. A Java object in a state that is in a temporary state is called a temporary object.

  Persistent State : has been persisted and added to the session's cache. A Java object in the persisted state is called a persisted object.

  Free State : It has been persisted, but not in the session's cache. A Java object that is in a free state is called a free object.

Two. Specific description

2.1 Code Description

@Test Public voidstatus () {//Open TransactionTransaction tx =session.begintransaction (); //Customer is in a temporary state at this time, customer does not hold OIDCustomer customer =NewCustomer (); Customer.setname ("NNNNNN"); Customer.setphonenum ("223344"); //at this point the object holds the OID, and the associated relationship with the database    intID =(Integer) session.save (customer); //when using the load () or get () method, no SQL statements are sent, but are obtained directly from the session cache. Customer Customer1 = Session.get (customer.class, id); //The result is true, which means that the same object is actually being obtainedSystem.out.println (Customer = =customer1); Customer.setname ("Yyyyyy"); Customer1.setname ("BBBBBB");              System.out.println (Customer1.getname ()); System.out.println ("Customer's ID is:" +Customer.getid ()); //when a transaction commits, it actually sends two SQL: one insert, one updateTx.commit (); //when the session is closed, customer is in a Free StateSession.close (); System.out.println ("When customer becomes Free State, still holds OID:" +Customer.getid ()); Session Session2=oraclesessionutils.opensession (); Transaction tx1=session2.begintransaction (); //at this point, the customer is re-incorporated into the session's cache, and the Free State becomes the persistent state.session2.update (customer); Customer.setname ("Mmmmmm"); Customer.setname ("Oooooo"); //the transaction is submitted again, and only one SQL is actually sentTx1.commit (); //customer changes from persistent state to free StateSession2.close ();
}

2.2 Picture Description

  Note: This image is also excerpted from FG2006, a blog post for the Great God.

2.3 Description of the relevant methods for object state transitions

Get, load, find: The method is more similar to the use of the Get and find methods the author has not found any differences at the moment. They all map data from the corresponding ID in the database to a Java object, where the object becomes persisted.

Save: Saved, at which point the Java object has a relationship with the database record. Changes an object from a temporary state to a persisted state or to a persisted state of data from a free state.

Saveorupdate: Save or update, if there is no OID corresponding to the database record, perform the save, and if there is, perform the update. Changes an object from a temporary state to a persisted state or to a persisted state of data from a free state.

Delete: Deletes an object from the persisted state or the Free State to a temporary state.

Close: Closes the session, clears the session first, and then closes it. Changes an object from a persistent state to a temporary state.

Clear: Empties the session cache. Changes an object from a persistent state to a temporary state.

Evict: Clears the specified object. Changes an object from a persistent state to a temporary state.

Three states of 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.