views:

71

answers:

2

Hey!

I made some "save" bean functionality in my Java Web Application (JSF1.2, RichFaces). It is using JAXB to turn it into an XML string and then it is stored in the database. If the user loads this back, I want to notify the user if the (bean)content is changed and it should be saved again.

My idea is to override the hashCode() function 'with' org.apache.commons.lang.builder.HashCodeBuilder, however I have lots of fields and child elements. Is there any other way to handle this kind of functionality?

EDIT

"Comparison" is done on another view!

Any help would be appreciated!

+4  A: 

You could add a boolean dirty; flag to the bean, set it to true in your setters and clear it during a reload and after a save.

rsp
Well, in your setters and in any other methods that modify any variables in any way. Or make sure that your setters are the only such methods.
MatrixFrog
Problem is that the user can submit the same values, causing all the setter methods to be called.
wheelie
That works if the setter examines whether the value has been changed. Still a lot of redundant code, but works.
wheelie
+1  A: 

You can't rely on hashCode alone to determine if an object has been changed. That is, just because the object has the same hashCode as before, does NOT mean that it has not been changed.

polygenelubricants