Persisting the state of an object

Source: Internet
Author: User
Tags flush

Standing in the perspective of persistence, hibernate divides objects into 4 states: persistent state, temporary state, Free State, and delete state. The specific method of the session allows the object to transition from one state to another.
   Temporary Object (Transient):
In the case of a surrogate primary key, the OID is usually null;
is not in the session cache;
There are no corresponding records in the database.
Typically, you create a new object with new and do not set its ID property value, which is the temporary object. Persisting Objects (also called "Hosting") (Persist):
OID is not null;
is located in the session cache;
If there is already a corresponding record in the database, the persisted object corresponds to the related record in the database;
Session in flush cache, according to the persistence of the properties of the object changes, to synchronize the update database;
In the cache of the same session instance, each record in the database table only corresponds to a unique persisted object. Free Objects (also known as "off-pipe") (Detached):
OID is not null;
is no longer in the session cache;
As a general rule, a free object is transformed by a persistent object, so there may be records in the database that correspond to it.

Delete Object (removed):
There is no record in the database corresponding to its OID;
is no longer in the session cache;
In general, applications should not use deleted objects anymore.

The object conversion diagram for these states is as follows:
  

 
Here's how the session can transform the state of an object:

Save ()
The Save () method of the Session turns a temporary object into a persisted object;
The Save () method of the Session completes the following actions:
1. Add the News object to the Session cache to allow it to enter a persistent state;
2. use the identifier generator specified by the mapping file to assign a unique OID to the persisted object.
(The SetId () method sets the OID for the News object to be invalid if the proxy primary key is used)
3. plan to execute an INSERT statement: During flush caching, Hibernate maintains its correspondence with database-related records by persisting the OID of the object, and when the News object is persisted, the program is not allowed to modify its id< /c4>.
 
persist () and save () differences:
When the Save () method is executed on an OID object that is not NULL, the object is saved to the database as a new OID ; However, an exception is thrown when the persist () method is executed.

get () and load ()
1. Both methods can load a persisted object from the database based on the given OID;
2. Execute Get method: The object will be loaded immediately; (Retrieve now)
Execute the Load method: If the object is not used, the query operation is not performed immediately, and a proxy object is returned until it is used to load. (Deferred search)
3. If the Session,load method is closed before the proxy object needs to be initialized, the lazyinitializationexception exception may be thrown (lazy loading exception). For example: The following code throws a lazy load exception:

public void TestLoad () {

        News News = (news) Session.load (News.class, 1);

        Session.close ();
        SYSTEM.OUT.PRINTLN (news); 
    }


The following code, however, can load normally:

public void TestLoad () {

        News News = (news) Session.load (News.class, 1);

        SYSTEM.OUT.PRINTLN (news);
        Session.close ();
        SYSTEM.OUT.PRINTLN (news); 
    }

4. If there is no record in the data table corresponding to the OID, and the session is not closed:
The Get method returns null;
If the Load method does not use any of the properties of the object, the problem does not occur, otherwise the objectnotfoundexception exception is thrown.

update ()
1. The update () method of the Session can turn a free object into a persisted object and plan to execute an UPDATE statement.
2. If you want the session to execute the update () statement only when the object's properties change, you can put the <class> element in the map file Select-before-update is set to True, the default value of this property is false. (If the object is a persisted object and its properties have not changed, the update operation will not occur even if Select-before-update is not configured)
3. If you update a persisted object, you typically do not need to display the call to the Update method, because the flush method of the session is executed first when the commit () method of transaction is called.
4. when the update () method associates a free object , if a persisted object with the same OID already exists in the cache of the Session, Throws an exception because it cannot have two OID objects in the session cache.
5. when the update () method associates a free object, an exception is thrown if no corresponding record exists in the database.

saveorupdate ()
The first is to determine whether the saved object is a temporary object or a free object, and if it is a temporary object, call the Save () method, or the update () method if it is a free object.
Criteria for the decision object as a temporary object:
Standard One: The OID of the Java object is null, NULL is a temporary object, otherwise it is a free object.
Standard Two: if the Unsaved-value property is set for <id> in the mapping file, the OID value equals the Unsaved-value property value of the Java object as a temporary object, otherwise it is a free object.
 
The saveorupdate execution process is as follows:
 

Delete ()
1. the Delete () method of the Session can either delete a free object or delete a persisted object. As long as the OID is corresponding to a record in the data table, the delete operation is prepared, and an exception is thrown if the OID does not have a corresponding record in the data table.
2. the Delete () method of the Session process is: Plan to execute a DELETE statement, remove the object from the Session cache, and then the object into the delete state.
3. by default, after the Delete method is executed, the object's ID value is still saved in the object, which can be inconvenient in some cases, such as the Saveorupdate () method for that object. Hibernate has a Hibernate.use_identifier_rollback property in the configuration file Hibernate.cfg.xml, with a default value of false, and if it is set to true, after the Delete () method is executed, The ID value of the object will be set to null.

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.