It is actually a very easy thing to do:
package mypackage;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.HibernateException;
import java.io.Serializable;
import java.security.SecureRandom;
import java.util.UUID;
public class RandomIdentifierGenerator implements IdentifierGenerator {
private final static SecureRandom sr = new SecureRandom();
public Serializable generate(SessionImplementor sessionImplementor, Object o) throws HibernateException {
long val = sr.nextLong();
return Long.toString(Math.abs(val), Character.MAX_RADIX);
}
}
IdentitfierGenerator
is the hibernate interface you have to implement. The above example just generates a random id.
In order to use this you have to set the generator
to mypackage.RandomIdentifierGenerator
Obviously this implementation lacks any guarantee of not generating the same id twice, this may or may not be important for the application you are writing.