Monday, February 16, 2009

Hibernate Unique Ids

It seems that hibernate does not really like working with unique ids in the database (we experiemented it when using the embedded version in Gigaspaces).

A way to overcome the issue, is adding a new field to the database, which will be used as unique field, and its value will be generated on the fly in the application level.

The unique value can be generated using the relevant API (GUID in MS) and UUID in Java:

import java.util.UUID;

public class GenerateUUID {
public static final void main(String... aArgs) {
//generate random UUIDs
UUID idOne = UUID.randomUUID();
UUID idTwo = UUID.randomUUID();
log("UUID One: " + idOne);
log("UUID Two: " + idTwo);
}
private static void log(Object aObject){
System.out.println( String.valueOf(aObject) );
}
}

No comments:

Post a Comment