Hibernate Persistence object status

Source: Internet
Author: User

From the perspective of persistence, Hibernate divides objects into four states: Persistence State, temporary state, free state, and delete state. The Session method can convert objects from one state to another.

Temporary object in the Persistent Object state (Transient ):
-When the proxy primary key is used, the OID is usually null-not in the Session cache-no corresponding records in the database
Persist ):
-OID is not null-it is located in the Session cache-if there is a corresponding record in the database, the persistence object corresponds to the relevant record in the database-when the Session is in the flush cache, the database will be synchronously updated based on the attribute changes of the Persistent Object-in the cache of the same Session instance, each record in the database table only corresponds to a unique Persistent Object

Removed)

-There is no record corresponding to its OID in the database-it is no longer in the Session cache-in general, the application should no longer use the deleted object

Detached ):
OID is not null
No longer in Session cache
In general, the free object is transformed from a persistent object, so there may be records corresponding to it in the database.


Object status transition diagram

The save () method of the save () and persist () sessions converts a temporary object to a persistent object.
The save () method of the Session completes the following operations:
-Add the News object to the Session cache, make it persistent-select the identifier specified by the ing file to generate a thick ky "http://www.bkjia.com/kf/ware/vc/" target = "_ blank" class = "keylink"> vcsIArOqrPWvsO7r7bUz/keys + sDt1ve8/LXEx + m/ 9 s/Second/second + 3b/iu + second + CgpIaWJlcm5hdGUgzai5/bPWvsO7r7bUz/Second/y6zcr9vt2/4S/Second/second + second/Second/second + keys + raotcQgT0lEILTTyv2 + 3b/keys/zo6zU8rK7u + keys + y2df3o6y2 + keys/PH + keys/b7dv + keys/keys + 0 + keys + samples/samples + 21 M/samples/ndcttzsdrtttp87rnyv2 + 3b/samples + G3osvNdXBkYXRl0 ++ samples/J0tSw0dOzyeTOxLz + samples + m/9 s/samples + + samples /PKsSwgyOe5 + 9425v2 + 3b/ready + rbTm1NrP4M2sIE9JRCC1xLPWvsO7r7bUz/weight + weight "http://www.2cto.com/uploadfile/Collfiles/20140602/20140602090506319.png" alt = "\">
Criteria for determining an object as a temporary object
-The OID of the Java object is null. The unsaved-value attribute is set, and the OID value of the Java object matches the unsaved-value attribute value.

1. if the OID is not null but there is no corresponding record in the data table, an exception is thrown. understanding: the object of the unsaved-value Attribute whose OID value is equal to id is also considered a free object.
Merge ()


Delete (): If the OID corresponds to a record in the data table, the delete operation is ready. If the OID does not have a corresponding record in the data table, the delete () method that throws an exception Session can delete both a free object and a persistent object.
Session's delete () method processing process-plan to execute a delete Statement (before submission, the object still has an OID and can still be used) -after submission, the object is deleted from the Session cache and enters the deletion state. cfg. the xml configuration file contains a hibernate. use_identifier_rollback attribute. Its default value is false. If it is set to true, the Operation Behavior of the delete () method is changed: delete () the method sets the persistence object or free object OID to null to make them temporary objects.
Evict () removes the specified persistent object from the session cache
Call the Work interface of the stored procedure through Hibernate: Access database operations directly through the JDBC API

The doWork (Work) method of the Session is used to execute the operation specified by the Work object, that is, the execute () method of the Work object is called. The Session will pass the currently used database connection to the execute () method.


When Hibernate and trigger work together with the trigger in the database, there are two problems.
-Trigger: The persistence object in the Session cache is inconsistent with the corresponding data in the database: the trigger runs in the database and the operations it performs are transparent to the Session.-Session update () method to blindly trigger: The update statement is executed regardless of whether the attributes of the Free object change. The update statement will trigger the corresponding trigger in the database.
Solution:
-After executing the Session-related operations, call the flush () and refresh () Methods of the Session immediately to force the Session cache to be synchronized with the database (refresh () method to re-load objects from the database)
-In Set the select-before-update attribute in the element: When the update or saveOrUpdate () method of the Session updates a free object, the Select statement is executed first, obtain the latest data of the current free object in the database. The update statement is executed only when the data is inconsistent.



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.