Skip to content

Instantly share code, notes, and snippets.

Created February 7, 2015 06:22
What would you like to do?
Custom Hibernate UUID Generator
// Before
@Column(name = "entity_id")
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String entityId;
// After
@Column(name = "entity_id")
@GeneratedValue(generator = "inquisitive-uuid")
@GenericGenerator(name = "inquisitive-uuid", strategy = "com.myapp.persistence.generators.InquisitiveUUIDGenerator")
private String entityId;
package com.myapp.persistence.generators;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.type.Type;
import java.util.Properties;
* Based on
public class InquisitiveUUIDGenerator extends UUIDGenerator {
private String entityName;
public void configure(Type type, Properties params, Dialect dialect) {
entityName = params.getProperty(ENTITY_NAME);
super.configure(type, params, dialect);
public Serializable generate(SessionImplementor session, Object object) {
Serializable id = session
.getEntityPersister(entityName, object)
.getIdentifier(object, session);
if (id == null) {
return super.generate(session, object);
} else {
return id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment