org.hibernate.id.UUIDGeneratorCustom Maven / Gradle / Ivy
package org.hibernate.id;
import java.io.Serializable;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;
/**
* @see org.hibernate.jpa.spi.IdentifierGeneratorStrategyProvider#getStrategies()
* @see org.hibernate.jpa.AvailableSettings#IDENTIFIER_GENERATOR_STRATEGY_PROVIDER
* @see org.hibernate.engine.spi.SessionImplementor
* @see org.hibernate.engine.spi.SharedSessionContractImplementor
*/
public class UUIDGeneratorCustom extends UUIDGenerator {
public static final String GENERATOR_STRATEGY = UUIDGeneratorCustom.class.getName();
private String entityName;
@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
this.entityName = params.getProperty(ENTITY_NAME);
super.configure(type, params, serviceRegistry);
}
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
Serializable serializable = session.getEntityPersister(this.entityName, object).getIdentifier(object, session);
return serializable == null ? super.generate(session, object) : serializable;
}
}