From 7f3b777cd195143eb3e99023e84ef8093f5285b2 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sat, 18 May 2024 20:10:17 +0200 Subject: [PATCH] HHH-18139 start trying to simplify things Signed-off-by: Gavin King --- .../boot/model/internal/GeneratorBinder.java | 2 +- .../internal/IdentifierGeneratorUtil.java | 85 +++++++++++++++++-- .../org/hibernate/mapping/SimpleValue.java | 6 +- 3 files changed, 82 insertions(+), 11 deletions(-) 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 e9f3755713..2299940e5a 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 @@ -362,8 +362,8 @@ public class GeneratorBinder { final Class annotationType = annotation.getAnnotationType(); final IdGeneratorType idGeneratorType = annotationType.getAnnotation( IdGeneratorType.class ); assert idGeneratorType != null; + final Class generatorClass = idGeneratorType.value(); return creationContext -> { - final Class generatorClass = idGeneratorType.value(); checkGeneratorClass( generatorClass ); final Generator generator = instantiateGenerator( annotation, diff --git a/hibernate-core/src/main/java/org/hibernate/id/factory/internal/IdentifierGeneratorUtil.java b/hibernate-core/src/main/java/org/hibernate/id/factory/internal/IdentifierGeneratorUtil.java index 212cb3970a..772c464178 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/factory/internal/IdentifierGeneratorUtil.java +++ b/hibernate-core/src/main/java/org/hibernate/id/factory/internal/IdentifierGeneratorUtil.java @@ -6,39 +6,110 @@ */ package org.hibernate.id.factory.internal; +import org.hibernate.InstantiationException; +import org.hibernate.MappingException; +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.Dialect; import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.config.spi.StandardConverters; +import org.hibernate.id.Assigned; +import org.hibernate.id.Configurable; +import org.hibernate.id.ForeignGenerator; +import org.hibernate.id.GUIDGenerator; import org.hibernate.id.IdentifierGenerator; +import org.hibernate.id.IdentityGenerator; +import org.hibernate.id.IncrementGenerator; import org.hibernate.id.OptimizableGenerator; import org.hibernate.id.PersistentIdentifierGenerator; +import org.hibernate.id.SelectGenerator; +import org.hibernate.id.UUIDGenerator; +import org.hibernate.id.UUIDHexGenerator; import org.hibernate.id.enhanced.LegacyNamingStrategy; +import org.hibernate.id.enhanced.SequenceStyleGenerator; import org.hibernate.id.enhanced.SingleNamingStrategy; -import org.hibernate.id.factory.IdentifierGeneratorFactory; +import org.hibernate.id.enhanced.TableGenerator; import org.hibernate.mapping.Column; import org.hibernate.mapping.RootClass; import org.hibernate.mapping.SimpleValue; import org.hibernate.mapping.Table; import org.hibernate.generator.Generator; +import java.lang.reflect.Constructor; import java.util.Map; import java.util.Properties; +import static org.hibernate.internal.util.ReflectHelper.getDefaultConstructor; + public class IdentifierGeneratorUtil { public static Generator createLegacyIdentifierGenerator( SimpleValue simpleValue, - IdentifierGeneratorFactory identifierGeneratorFactory, Dialect dialect, String defaultCatalog, String defaultSchema, RootClass rootClass) { - return identifierGeneratorFactory.createIdentifierGenerator( - simpleValue.getIdentifierGeneratorStrategy(), - simpleValue.getType(), - collectParameters( simpleValue, dialect, defaultCatalog, defaultSchema, rootClass ) - ); + final Class generatorClass = generatorClass( simpleValue ); + final Constructor defaultConstructor = getDefaultConstructor( generatorClass ); + if ( defaultConstructor == null ) { + throw new InstantiationException( "No default constructor for id generator class", generatorClass ); + } + final Generator identifierGenerator; + try { + identifierGenerator = defaultConstructor.newInstance(); + } + catch (Exception e) { + throw new InstantiationException( "Could not instantiate id generator", generatorClass, e ); + } + if ( identifierGenerator instanceof Configurable ) { + final Properties parameters = collectParameters( simpleValue, dialect, defaultCatalog, defaultSchema, rootClass ); + final Configurable configurable = (Configurable) identifierGenerator; + configurable.configure( simpleValue.getType(), parameters, simpleValue.getServiceRegistry() ); + } + return identifierGenerator; + } + + private static Class generatorClass(SimpleValue simpleValue) { + String strategy = simpleValue.getIdentifierGeneratorStrategy(); + if ( "native".equals(strategy) ) { + strategy = + simpleValue.getMetadata().getDatabase().getDialect() + .getNativeIdentifierGeneratorStrategy(); + } + switch (strategy) { + case "assigned": + return Assigned.class; + case "enhanced-sequence": + case "sequence": + return SequenceStyleGenerator.class; + case "enhanced-table": + case "table": + return TableGenerator.class; + case "identity": + return IdentityGenerator.class; + case "increment": + return IncrementGenerator.class; + case "foreign": + return ForeignGenerator.class; + case "uuid": + case "uuid.hex": + return UUIDHexGenerator.class; + case "uuid2": + return UUIDGenerator.class; + case "select": + return SelectGenerator.class; + case "guid": + return GUIDGenerator.class; + } + final Class clazz = + simpleValue.getServiceRegistry().requireService( ClassLoaderService.class ) + .classForName( strategy ); + if ( !Generator.class.isAssignableFrom( clazz ) ) { + // in principle, this shouldn't happen, since @GenericGenerator + // constrains the type to subtypes of Generator + throw new MappingException( clazz.getName() + " does not implement 'Generator'" ); + } + return clazz; } public static Properties collectParameters( diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java index 061cf54163..b0bb403044 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java @@ -402,7 +402,7 @@ public abstract class SimpleValue implements KeyValue { ); } else { - generator = createLegacyIdentifierGenerator(this, identifierGeneratorFactory, dialect, null, null, rootClass ); + generator = createLegacyIdentifierGenerator(this, dialect, null, null, rootClass ); if ( generator instanceof IdentityGenerator ) { setColumnToIdentity(); } @@ -872,7 +872,7 @@ public abstract class SimpleValue implements KeyValue { @Override public boolean isColumnInsertable(int index) { - if ( insertability.size() > 0 ) { + if ( !insertability.isEmpty() ) { return insertability.get( index ); } return false; @@ -880,7 +880,7 @@ public abstract class SimpleValue implements KeyValue { @Override public boolean isColumnUpdateable(int index) { - if ( updatability.size() > 0 ) { + if ( !updatability.isEmpty() ) { return updatability.get( index ); } return false;