diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java index 0905084b8f..77f399b4c9 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java @@ -68,6 +68,8 @@ import org.hibernate.resource.beans.container.spi.BeanContainer; import org.hibernate.resource.beans.spi.BeanInstanceProducer; import org.hibernate.resource.beans.spi.ManagedBeanRegistry; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.type.Type; import org.jboss.logging.Logger; import jakarta.persistence.GeneratedValue; @@ -83,40 +85,55 @@ import static org.hibernate.boot.model.internal.BinderHelper.isGlobalGeneratorNa import static org.hibernate.internal.util.ReflectHelper.getDefaultConstructor; import static org.hibernate.mapping.SimpleValue.DEFAULT_ID_GEN_STRATEGY; +/** + * Responsible for configuring and instantiating {@link Generator}s. + * + * @author Gavin King + */ public class GeneratorBinder { private static final Logger LOG = CoreLogging.logger( BinderHelper.class ); + /** + * Create an id generator for the given named {@code strategy} using the + * "old" (pre-Hibernate 6) approach. + */ public static Generator createLegacyIdentifierGenerator( String strategy, - SimpleValue simpleValue, + SimpleValue idValue, Dialect dialect, RootClass rootClass, Map configuration) { - final Class generatorClass = generatorClass( strategy, simpleValue ); - final Constructor defaultConstructor = getDefaultConstructor( generatorClass ); - if ( defaultConstructor == null ) { - throw new org.hibernate.InstantiationException( "No default constructor for id generator class", generatorClass ); - } - final Generator identifierGenerator; - try { - identifierGenerator = defaultConstructor.newInstance(); - } - catch (Exception e) { - throw new org.hibernate.InstantiationException( "Could not instantiate id generator", generatorClass, e ); - } + final Generator identifierGenerator = instantiateGenerator( generatorClass( strategy, idValue ) ); if ( identifierGenerator instanceof Configurable ) { - final Properties parameters = collectParameters( simpleValue, dialect, rootClass, configuration ); final Configurable configurable = (Configurable) identifierGenerator; - configurable.configure( simpleValue.getType(), parameters, simpleValue.getServiceRegistry() ); + configurable.configure( idValue.getType(), + collectParameters( idValue, dialect, rootClass, configuration ), + idValue.getServiceRegistry() ); } return identifierGenerator; } - private static Class generatorClass(String strategy, SimpleValue simpleValue) { + private static Generator instantiateGenerator(Class generatorClass) { + final Constructor defaultConstructor = getDefaultConstructor( generatorClass ); + if ( defaultConstructor == null ) { + throw new org.hibernate.InstantiationException( "No default constructor for id generator class", generatorClass); + } + try { + return defaultConstructor.newInstance(); + } + catch (Exception e) { + throw new org.hibernate.InstantiationException( "Could not instantiate id generator", generatorClass, e ); + } + } + + /** + * Interpret an "old" generator strategy name as a {@link Generator} class. + */ + private static Class generatorClass(String strategy, SimpleValue idValue) { if ( "native".equals(strategy) ) { strategy = - simpleValue.getMetadata().getDatabase().getDialect() + idValue.getMetadata().getDatabase().getDialect() .getNativeIdentifierGeneratorStrategy(); } switch (strategy) { @@ -145,7 +162,7 @@ public class GeneratorBinder { return GUIDGenerator.class; } final Class clazz = - simpleValue.getServiceRegistry().requireService( ClassLoaderService.class ) + idValue.getServiceRegistry().requireService( ClassLoaderService.class ) .classForName( strategy ); if ( !Generator.class.isAssignableFrom( clazz ) ) { // in principle, this shouldn't happen, since @GenericGenerator @@ -155,6 +172,10 @@ public class GeneratorBinder { return clazz; } + /** + * Collect the parameters which should be passed to + * {@link Configurable#configure(Type, Properties, ServiceRegistry)}. + */ public static Properties collectParameters( SimpleValue simpleValue, Dialect dialect, @@ -279,16 +300,12 @@ public class GeneratorBinder { + " (define a named generator using '@SequenceGenerator', '@TableGenerator', or '@GenericGenerator')" ); } //This is quite vague in the spec but a generator could override the generator choice - //yuk! this is a hack not to override 'AUTO' even if generator is set - final boolean avoidOverriding = - definition.getStrategy().equals( "identity" ) - || definition.getStrategy().equals( "seqhilo" ); - if ( generatorType == null || !avoidOverriding ) { - generatorStrategy = definition.getStrategy(); - } - else { - generatorStrategy = generatorType; - } + generatorStrategy = + generatorType == null + //yuk! this is a hack not to override 'AUTO' even if generator is set + || !definition.getStrategy().equals( "identity" ) + ? definition.getStrategy() + : generatorType; //checkIfMatchingGenerator(definition, generatorType, generatorName); parameters.putAll( definition.getParameters() ); } @@ -642,7 +659,7 @@ public class GeneratorBinder { } } } - catch (InvocationTargetException | InstantiationException | IllegalAccessException | IllegalArgumentException e ) { + catch (InvocationTargetException | InstantiationException | IllegalAccessException | IllegalArgumentException e) { throw new HibernateException( "Could not instantiate generator of type '" + generatorClass.getName() + "'", e diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/entities/Furniture.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/entities/Furniture.java index d37e449397..59e7e85bf2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/entities/Furniture.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/entities/Furniture.java @@ -23,12 +23,7 @@ public class Furniture { @Id @GeneratedValue(generator = "hibseq") - @GenericGenerator(name = "hibseq", strategy = "seqhilo", - parameters = { - @Parameter(name = "max_lo", value = "5"), - @Parameter(name = "sequence", value = "heybabyhey") - } - ) + @GenericGenerator(name = "hibseq", strategy = "increment") public Integer getId() { return id; } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/sequences/entities/Furniture.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/sequences/entities/Furniture.java index 4673ce441e..97ef65b78c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/sequences/entities/Furniture.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/id/sequences/entities/Furniture.java @@ -23,12 +23,7 @@ public class Furniture { @Id @GeneratedValue(generator = "hibseq") - @GenericGenerator(name = "hibseq", strategy = "seqhilo", - parameters = { - @Parameter(name = "max_lo", value = "5"), - @Parameter(name = "sequence", value = "heybabyhey") - } - ) + @GenericGenerator(name = "hibseq", strategy = "increment") public Integer getId() { return id; }