Hibernate dirty Data Check

Source: Internet
Author: User

Dirty data check: What is dirty data? Dirty data is not obsolete and useless data, but changes before and after the state of the data. Let's look at the following code: Transaction tx=session.begintransaction (); user user= (user) Session.load (user.class, "1");// load eligible data from the database user.setname ("ZX");// Change the name attribute of the user object, at which point the user object becomes the so-called "dirty data" tx.commit (); When a transaction commits,Hibernate pairs The PO in the session ( the persisted object ) is checked to see if the state of the persisted object has changed, and if a change is made, the change is updated to the database. Here's a questionof howHibernate Determines whether an entity object has changed before and after the state. that is how Hibernate checks out a data that has become dirty. usually there are two ways to check dirty data: A, data Object monitoring: data Object monitoring is implemented through interceptors that monitor the setter methods of data Objects , similar to the concept of triggers in a database, when an object's properties call a setter method, the interceptor captures the action and flags the change to the changed property, which is updated to the database after the database operation. The advantage of this method is to improve the synchronization of the data update, but this is also its disadvantage, if an entity object has a lot of properties have changed, it is bound to cause a lot of interceptor callback method calls, these interceptors are through the Dynamic Proxy or CGLIB implementation will pay a certain cost of execution, so it is possible to cause a large delay in the update operation. B, the data version of the comparison : This approach is to save the most recently read version of the data object in the persistence framework, compare the submitted data to the saved version when the data is submitted, and synchronize it with the new database if the discovery changes. This approach reduces the real-time nature of synchronous updates, but when many of the properties of a data object change, because the persistence layer framework cache exists, the cache can be fully utilized when compared to the version, which reduces the delay in updating the data. in hibernate , the data version comparison method is used to check the dirty data, we use the following code to explain Hibernate 's implementation strategy. Transaction tx=session.begintransaction (); user user= (user) Session.load (user.class, "1"); User.setname ("ZX"); tx.commit (); when calling tx.commit (); when the show starts, commit () method will call the Session.flush () method, in the call Flush () method is called first, the flusheverything () to perform some preprocessing, such as invoking Intercepter, Complete the cascade operation, and so on), and then call flushentities () method, which is the key to the dirty data check. before I go on, I'm going to introduce an internal data structure.Entityentry,entityentryis from belonging toSessionimpl (Sessionimplementation class of the interface)the inner class, each oneEntityentrysaved the last entity synchronization with the original state information (such as: Entity version information, Entity lock mode, entity property information, etc.). ExceptEntityentrystructure, there is also a structure, a structure calledentityentries, it is alsoSessionimplthe inner class, and is aMaptype, which takes"Key-value"Save all the forms that are associated with the currentSessionthe Entity object and the raw state information associated with the instance, whereKeyis an entity object,valueis aEntityentry. andflushentities ()'s job is to traverseEntityentities,The entity object is compared with the original version to determine whether the entity object has changed. flushentities ()First, you will judge the entity'sIDIf a change occurs , the exception is considered to have occurred because the current entity andEntityentrythe corresponding relationship is illegal. If there is no exception and the version is determined to be a change in the entity properties, a new update task is added to the current Update task queue, which will beSession.flush ()method in theExecute ()method is converted to the correspondingSQLstatements are sent to the database for execution. FinallyTransactionwill call the currentSessioncorresponding to theJDBC Connectionof thecommit ()method commits the current transaction. dirty data checking occurs when a display saves an entity object, and the so-called display save refers to the explicit use in codeSessioncalledsave,update,saveorupdatemethod to save an entity object, such as:session.save (user);but sometimes, because of cascading operations, there is a problem, such as when saving aUserobject, it is based on theUserThe state of the object to which he is associated.Addressobject is saved, but the statement is not saved according to the display of the cascading object at this time. This is requiredHibernateyou can determine whether you want to save the cascading object to the database based on the state of the current object. At this point,Hibernatewill be based onUnsaved-valuebe judged. HibernateThe target object is first taken out of theID, and then theIDwith theUnsaved-valuevalues are compared, and if they are equal, the entity object is considered to have not been saved and will be saved immediately, otherwise the entity object is considered to have been saved without having to save again. For example, when aUserobject to add a new one to its associatedAddressobject, when thesession.save (user)when theHibernatewill be based onUnsaved-valuevalue to determine whichAddressobjects need to be saved, for newly addedAddressObject It'sIDhas not been assigned a value, asNULL, withUnsaved-valuevalues are equal, soHibernatetreats it as an unsaved object, creatingInsertstatement to be saved. If you want to useUnsaved-valuemust be configured as followsAddressobject thatIDProperties: ... <id name= "id" type= "java.lang.Integer" unsaved-value= "null" > <generator class= "increment"/> </id> ...

Hibernate dirty Data Check

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.