4 Replies Latest reply on Feb 12, 2009 5:20 PM by 807588

    Object Checksum or Reliable deep Clone and equals?

      Hi I am trying to check to see if an object has changed during execution.

      The simplest option would of course be to use a flag e.g: changed, however the objects are entity beans and contain collections therefore it would be difficult to enforce this changed flag on those objects as they can be "got" and manipulated directly.

      What would be useful would be if I could create a checksum of the object to start and then compare that later. Or another way would be through a deep clone using seriliization however when comparing the two objects (original and clone) they always appear to be different even if nothing has changed.

      So in summary is it possible to apply a checksum to an object?
        • 1. Re: Object Checksum or Reliable deep Clone and equals?
          This all sounds rather unwieldy. Yes, it would be possible to come up with some sort of "checksum" if you craft your objects to support it, but is this really the best approach?

          What do the objects represent and why do you need to track when changes have occurred? You may be asking the wrong question.
          • 2. Re: Object Checksum or Reliable deep Clone and equals?
            Thanks for the reply, the objects represent entities within a database (using JPA) they are bound to objects on a form and I wish to ensure that if the user has made changes they are prompted if they wish to save these changes.

            The solution I have come up with is this...
                public static byte[] getObjectBytes(Object obj) throws java.io.IOException {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(bos);
                    byte[] data = bos.toByteArray();
                    return data;
                public static String getMD5(byte[] bytes) {
                    String tempString = null;
                    try {
                        // Create an instance of the MD5 hasher.
                        MessageDigest md5Hasher = MessageDigest.getInstance("MD5");
                        // Set the parameters for the hashing algorithm.
                        md5Hasher.update(bytes, 0, bytes.length);
                        // Hash the password.
                        tempString = new BigInteger(1, md5Hasher.digest()).toString(16);
                    } catch (NoSuchAlgorithmException ex) {
                        Logger.getLogger(ObjectCloner.class.getName()).log(Level.SEVERE, null, ex);
                    // Return the encoded string.
                    return tempString;
                public static String md5EncodeObject(Object o) {
                    try {
                        return getMD5(getObjectBytes(o));
                    } catch (IOException ex) {
                        Logger.getLogger(ObjectCloner.class.getName()).log(Level.SEVERE, null, ex);
                    return null;
            By calling md5EncodeObject I can get a hash from converting the object to a byte array and then getting the hash on it. I then save this string to a variable when the user goes to exit the form I check this again and prompt them if the new value differs from the old. It quick and a little bit hacky but for the moment it seams to work...!

            Any further suggestions are warmly welcomed...


            • 3. Re: Object Checksum or Reliable deep Clone and equals?
              What do you mean exactly by "bound to objects on a form" ?

              In this sort of scenario I'd expect the form handling logic to track whether changes have been made.

              (Edit) The problem I have with the approach you're considering is not that it won't work, but that it's rather inefficient. You have logic updating the objects, so why isn't that logic keeping track of this?

              Edited by: dcminter on 12-Feb-2009 13:28
              • 4. Re: Object Checksum or Reliable deep Clone and equals?
                I am using beans binding these are being generated by Netbeans I also have 40+ entities that need to do pretty much the same and just over a month to develop v1 of the app, its a desktop app so its not going to make any difference to usability. This way is quick and dirty and should work for all my objects. Thanks. P