(vii) The state of the Java object in the Hibernate persistence layer

Source: Internet
Author: User

I. Overview

-Temporary state: The object has just been created with the new statement and is not persisted and is not in the session cache. A Java object 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 becomes a persisted object.

-Free State: has been persisted, but is no longer in the session cache. A Java object that is in a free state becomes a free object.

Characteristics of a temporary object:        -oid is null        -not in the session's cache, or it can be said that it is not associated with any one session instance        -there is no corresponding record of the persisted object's characteristics in the database:        -oid is not null        -in the cache of a session instance, the persisted object is always associated with a session instance        -The persisted object corresponds to the related record in the database        -the database is updated synchronously based on the properties of the persisted object.            user  user= (user) Session.get (user.class,1);//Gets Persistent object persistent state            user.setname ("Jerry");            Transaction.commit ();        We found that we did not execute the UPDATE statement, but we printed the UPDATE statement. Hibernate automatically synchronizes the persisted object's state to the database. Characteristics of the Free object:        -oid is not null        -no longer in the session cache, it can be said that the free object is not associated with the session        

 

Example:

        The process of object state transition        Transaction ts=session.begintransaction ();        User User =new User ();                  Temporary status         user.setname ("Tom");         User.setpassword ("123456");        Session.save (user);                     Transition to persistent state        ts.commit ();                            Persistent state         session.close ();                        Transition to Free State        sessionfactory.close ();        System.out.println (User.getname ());     
conversion of two or three states

    1) Temporary state transition Persistence State-session the Save () method converts the staging state to a persistent state. Put the object you want to save into the session cache, and put it into a persistent state.            Assigns a unique OID to a persisted object using the primary key generation policy specified by the mapping file. The Save method simply assigns the UID to the object.            We can break the point at the Save method. When our primary key generation strategy is native, because we use MySQL database, the primary key is self-increment, so after executing the Save method, print the INSERT statement, MySQL database for our object auto-increment when our primary key generation policy is Incrementt, incre ment is maintained by hibernate, first go to the table to check the maximum ID then +1, after we execute the Save method, we find the statement printing select find the maximum ID, and then print the INSERT statement 2) temporary state into a free state-the temporary state of the object OID is set to        The corresponding record in the database.        User User=new user ();    User.setid (1); 3) The persistence state transitions to a temporary state first: User user= (user) Session.get (user.class,1);//Get Persisted Object Persistent state session.close () ; Free State user.setid (NULL);//Temporary state second: User user= (user) Session.get (user.class,1);//Get persisted object persistence State session.evict (user); Free State, this method clears the session cache to the persisted object, making it a Free State user.setid (NULL);//Temporary state 4) The persistence state transitions to the Free State first: Call the session's Close method, Persistent state becomes Free State second: Call the session's evict () method, turn the persistent state to Free State 5) The Free State is converted to a temporary state, only the object OID of the Free State is changed to NULL.   6) The Free State is converted to a persistent state session by the update () method to convert the Free State to a persistent state. User user= (user) Session.get (user.class,1);//Get Persisted Object Persistent state session.evict (user); Free State, this method clears the persistent object from the session cache and makes it a Free State session.update (user);

  

  

 

(vii) The state of the Java object in the Hibernate persistence layer

Related Article

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.