Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if a Java-object has been modified?

Tags:

java

Consider that I’ve got a standard Java bean: i.e. it contains members that are String, List, HashMap, etc.

My question is: what’s the easiest way to detect if an instance of such an object has been modified from say a previous/original state?

The reason I want to know this is so I determine whether I should update the object in the DB, or not, in the case where either: (i) no changes were made, or (ii) changes were made but then reversed.

I’ve been thinking about comparison of hashCode, or byte[], but doesn’t seem to work. Any ideas?

like image 402
Larry Avatar asked Nov 14 '11 15:11

Larry


3 Answers

Yes, it can be done, and it has been done... it's called hibernate. Just use that and don't "reinvent the wheel"

like image 185
Bohemian Avatar answered Sep 24 '22 07:09

Bohemian


if you have setters, notify a list of listeners in this setters if a new value is set.

like image 24
Thomas Uhrig Avatar answered Sep 24 '22 07:09

Thomas Uhrig


The same problem had hibernate developers, and they've dealt with it by creating proxies for returned entities. The proxy registers each setter call and so determines if entity has changed.

like image 24
Danubian Sailor Avatar answered Sep 26 '22 07:09

Danubian Sailor