Don't Let Hibernate Steal Your Identity
Subject:   Version usage
Date:   2006-11-09 19:44:11
From:   jbrundege
Response to: Version usage

No, the version should not be part of equals() and hashcode(). The version changes every time you save, so if it were part of hashcode() your sets would lose the object just because you saved it. Version changes much to often to be part of equals() and hashcode(), which should only use fields that rarely or never change.

The suggested definition of equals() is that if two objects represent the same row in the database, they should be equal. For that we just need the id (which is the database primary key). They may not have identical data, but if you save one its data will go in the database. If you try to save the other one you will get a ConcurrentModificationException (that's what the version does), which is appropriate to keep two people from stepping on each other's data. Hence to Java the objects are equal because they are two representations of the same database row, but Hibernate will know not to allow one such object to overwrite another one.