Hibernate Session Object Core method

Source: Internet
Author: User

1. Persist the state of the object:

Standing in the perspective of persistence, Hibernate divides objects into four states: persistent state, temporary state, Free State, delete state

Specific methods of the Session can move an object from one state to another

Temporary objects:

OID is usually null in the case of using a proxy primary key

Not in the Session's cache

There are no corresponding records in the database

Persisted objects (also called managed):

OID is not NULL

In the Session cache

If there is already a record in the database that corresponds to it, the persisted object corresponds to the related record in the database

Session updates the database synchronously, depending on the properties of the persisted object, when the flush cache

In the cache of the same Session instance, each record in the database table only corresponds to a unique persisted object

To delete an object:

There are no records in the database that correspond to their OID

is no longer in the Session's cache

In general, applications should not use deleted objects

Free object (also known as "off-tube"):

OID is not NULL

is no longer in the Session cache

In general, a free object is transformed by a persisted object, so there may be records in the database that correspond to it

The method in Session:

Save method:

1. is a temporary object becomes persisted object

2. Assigning an ID to an object

3. An INSERT statement is sent when flush refreshes the cache

4. The ID that was configured before the Save method is not valid

5. The ID of the persisted object cannot be modified

Persist method:

Similar to the Save method

Difference from the Save method: The Persist method throws an exception if the object already has an ID before calling the Persist method, and save does not

Get & Load Method:

Both the get and load methods can fetch records from the database

Difference:

1. Execute the Get method: The object is loaded immediately

Execute the Load method: If the object is not used, the query operation is not performed immediately and a proxy object is returned

Get is immediately retrieved

Load is a deferred retrieval

2. If there is no corresponding record in the datasheet

Get returns Null

Load throws an exception when using the object (because the Load method starts by returning a proxy object, so if no records are found, an exception is thrown)

3. Load method may throw lazy load exception

For example, the Session was closed before the proxy object was initialized

  

Update method:

1. If you update a persisted object, you do not need to explicitly call the Update method, because the flush method of the Session is executed first when the commit method of the Transaction is called

2. Updating a free object requires an explicit call to the Session's Update method. The Update method can turn a free object into a persisted object.

Attention:

1. The UPDATE statement is sent regardless of whether the record of the free object to be updated is consistent with the data table.

You can set Select-before-update=true (default = False) by setting the class node of the. hbm.xml file, but you typically do not need to set this property

2. If there is no corresponding record in the data table, but the Update method is called, an exception is thrown

3. When the Update method associates a free object, an exception is thrown if a persisted object of the same OID already exists in the cache of the session, because there cannot be two OID objects in the session buffer!

Saveorupdate Method:

This method also contains the functions of the Save and update methods.

If the object is a free object, the Update method is executed

If the object is a temporary object, the Save method is executed

Criteria for determining whether an object is a free object or a temporary object: If the OID of the Java object is null

Note: An exception is thrown if the OID of the object is not NULL, but the data table does not have its corresponding record.

Understanding: The OID value equals the ID of the Unsaved-value property value of the object and is also considered to be a free object

Delete method:

Performs a delete operation, which is prepared to perform a delete operation as long as the OID is corresponding to a record in the data table, and throws an exception if the OID does not have a corresponding record in the datasheet

After the object is deleted, the update and save operations cannot be performed because the OID is also saved in the object, but you can set the Hibernate.use_identifier_rollback property in the Hibernate configuration file to true so that after the object is deleted , the OID of the object is set to null

    

Evict Method:

Removes the specified persisted object from the Session cache

Hibernate Session Object Core method

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.