ORM

Hibernate with Business Keys

Posted on by  
Ronald Koster

A common practice with Hibernate is to give each entity an id field to store its primary key. This field is generally managed by Hibernate. When the entity has not been saved yet, the field is null, which is also how Hibernate detects the object has not been saved yet. Once it is saved, Hibernate sets the value of the id to the generated value. Since the value of the id field changes, it is not suited to base your equals and hashCode methods on. Which is a bit counter-intuitive, because it corresponds to the primary key in the database. The solution is to use Business Keys.

Continue reading →

shadow-left