Hibernate

Eclipse Generated Equality

4 minute read Published:

Most users of Eclipse are familiar with its code generation features. One of the more common of these is “Generate hashCode() and equals()…” from the right-click -> Source (⌘-Opt-S) menu. It roughly follows the recipe on page 33 of Joshua Bloch’s Effective Java, and it is effective in most situations. One case where it doesn’t work is when the class is proxied. Hibernate is just one example of a framework that generates proxies. If the Person class below were proxied, the default Eclipse implementation of equals() would break. In a Hibernate application, this can lead to anything from unnecessary deletes and inserts to some very frustrating bugs.

Hibernate: Automatic Versioning

3 minute read Published:

One of the optimistic locking features that Hibernate provides is automatic versioning. Versioned objects need to contain either a version or timestamp property that Hibernate uses to detect concurrent modifications at flush time. Hibernate’s versioning mechanism works by checking the number of rows that were affected by an update statement. Consider an accounts table like the one below.