Women in Technology

Hear us Roar



Article:
  Don't Let Hibernate Steal Your Identity
Subject:   hibernate id vs UUID
Date:   2009-01-02 14:01:31
From:   cgerritsen
You say that


"While Hibernate does have the option of generating UUIDs for you, we're back to the same old problem: your objects will not receive an ID at the time of creation, and will have to wait until they're saved."


No need to use a separate UUID to keep track of identity when persistence seals identity, so long as you reserve one value for id as meaning that the object is not persisted. An unpersisted object (one for which hibernate has not yet set the generated id) does not share identity with any other objects and is not equal to any other object besides itself.


The "unsaved id" (-1 for example) is the important point. The object's id is initialized to the unsaved id at object creation time. The value has to be one that will never be generated by your id generation algorithm. When the object is persisted, Hibernate sets the id to the generated id. Then, .equals starts by checking if the id has the unsaved value. If the object's id has the unsaved value, then equality is based on object1 == object2. If object1 and object2 are not the same object, then the two objects when persisted will not represent the same row in the database (hibernate will give them different ids at persistence time), and they are not equal.

Main Topics Oldest First

Showing messages 1 through 1 of 1.

  • hibernate id vs UUID
    2009-01-07 04:17:04  Bulat_ju [View]

    The problem is that when you base .equals and .hashcode on mutable properties you break Collections framework contract. And some types of collections stop working. So this article provides a workaround for that issue.

    If you don't need collections (especially Set collection, Map collection, etc.), then you have no problem. :) If you don't make an object previously stored in a collection persistent, then you again have no problem. etc. etc. etc.

    P.S. All the cases when you need this workaround and why one might need it are described at the beginning of the article.