What is the best way to determine if an object has been modified?

I made some "save" bean functions in my Java web application (JSF1.2, RichFaces). It uses JAXB to turn it into an XML string, and then it is stored in the database. If the user downloads it back, I want to notify the user if the content (bean) is changed and it needs to be saved again.

My idea is to override the hashCode() 'function with org.apache.commons.lang.builder.HashCodeBuilder , however, I have many fields and children. Is there any other way to deal with such features?

EDIT

The β€œcomparison” is performed in a different view!

Any help would be appreciated!

+4
source share
3 answers

You can add the boolean dirty; flag boolean dirty; in bean, set it to true in your setters and clear it during reboot and after saving.

+5
source

You cannot rely solely on hashCode to determine if an object has been modified. That is, just because the object has the same hashCode as before does NOT mean that it has not been modified.

+1
source

You can use Java MD5 functionality:

 MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); digest.update(myObject); byte[] hash = digest.digest(); 
-1
source

Source: https://habr.com/ru/post/1302575/


All Articles