Hibernate dirty data check

Source: Internet
Author: User

Dirty data check: What is dirty data? Dirty data is not discarded or useless data, but data that changes before and after the state. Let's look at the following code: Transaction Tx = session. begintransaction (); User user = (User) Session. Load (user. Class, "1 ");// Load qualified data from the database User. setname ("ZX ");// Changed User Object Name attribute. User Object becomes the so-called "dirty data" TX. Commit ();   When a transaction is committed, Hibernate Yes Session In Po ( Persistent Object ) Check whether the status of the Persistent object has changed. If yes, the changes will be updated to the database. There is a problem here, Hibernate How to determine whether the status of an object changes before and after. That is to say Hibernate Is how to check that a data has become dirty. There are two ways to check dirty data: A , Data Object monitoring: Data Object monitoring uses the Interceptor Setter This is similar to the trigger concept in the database. When the attribute of an object is called Setter The method changes. In this case, the interceptor will capture this action, mark the attribute change as changed, and update it to the database during subsequent database operations. The advantage of this method is that it improves the synchronization of data updates, but this is also its disadvantage. If a entity object has many attributes changed, it will inevitably lead to a large number of calls to the interceptor callback method, these interceptors all pass through Dynamic proxy Or Cglib Implementation, will pay a certain execution price during execution, so it may cause a large delay of the update operation. B , Data version Comparison : This method saves the latest read version of the data object in the persistence framework. when the data is submitted, the submitted data is compared with the saved version, if any change is found, synchronize it with the new database. This method reduces the timeliness of synchronous updates. However, when many attributes of a Data Object change, the persistence layer framework cache can make full use of the cache when comparing versions, this reduces the delay in updating data. In Hibernate We use the data version comparison method to check the dirty data. Hibernate . Transaction Tx = session. begintransaction (); User user = (User) Session. Load (user. Class, "1 "); User. setname ("zx "); TX. Commit (); When TX. Commit (); This is the starting point of the fun, Commit () Method call Session. Flush () Method. Flush () Method is called first. Flusheverything () For some preprocessing (such as calling Intercepter, Complete cascade operations), and then call Flushentities () Method, which is the key to dirty data check. Before continuing, I will introduce an internal data structure. Entityentry, entityentry Yes Sessionimpl (Session Interface implementation class ) Internal class, each Entityentry Stores the original state information of the last object synchronized with the database (such as the version information of the object, the locking mode of the object, and the attribute information of the object ). Besides Entityentry In addition to the structure, there is also a structure called Entityentries , It is also Sessionimpl And is Map Type, it uses "Key-value" Save all Session The object and original status information associated with the instance. Key Is an object, Value Yes Entityentry . While Flushentities () The job is to traverse Entityentities, Compare the object with the original version to determine whether the object has changed. Flushentities () First, the entity's ID Whether a change has occurred. If a change has occurred, it is deemed that an exception has occurred because the current entity and Entityentry The corresponding link of is invalid. If no exception occurs and the object attribute changes after version comparison, a new update task is added to the current update task queue. Session. Flush () Method Execute () In a method call SQL The statement is executed by the database. Last Transaction The current Session Corresponding JDBC connection Of Commit () Method to commit the current transaction. Dirty data check occurs when the object is displayed and saved. The so-called "display and save" is explicitly used in the code. Session Call Save, update, saveorupdate Method To save object, such: Session. Save (User ); However, sometimes, due to the existence of cascade operations, a problem may occur, for example, when you save User Object User Object status Address The object is saved, but the statement is not saved Based on the display of cascading objects. In this case Hibernate You can determine whether to save cascading objects to the database based on the status of the current object. At this time, Hibernate According Unsaved-Value . Hibernate The ID , And then ID And Unsaved-Value Values are compared. If they are equal, the object is considered to have not been saved, and then saved immediately. Otherwise, the object has been saved and does not need to be saved again. For example User The object is added to Address When Session. Save (User) , Hibernate According Unsaved-Value To determine which Address The object needs to be saved. Address Object ID Not assigned. Null , And Unsaved-Value The value is equal, so Hibernate Will regard it as an unsaved object, generate Insert Statement. If you want to use Unsaved-Value The following configurations are required: Address Object ID Attribute: ...... <ID name = "ID" type = "Java. Lang. Integer" unsaved-value = "null"> <Generator class = "increment"/> </ID> ......

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.