diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootMetadataBuilder.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootMetadataBuilder.java index 11a193e7274e7a..520c8c4d25f5a5 100644 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootMetadataBuilder.java +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootMetadataBuilder.java @@ -43,14 +43,12 @@ import org.hibernate.boot.model.process.spi.MetadataBuildingProcess; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; -import org.hibernate.boot.registry.selector.spi.StrategySelector; import org.hibernate.boot.spi.MetadataBuilderContributor; import org.hibernate.boot.spi.MetadataBuilderImplementor; import org.hibernate.cache.internal.CollectionCacheInvalidator; import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.dialect.spi.DialectFactory; -import org.hibernate.id.factory.IdentifierGeneratorFactory; import org.hibernate.integrator.spi.Integrator; import org.hibernate.internal.EntityManagerMessageLogger; import org.hibernate.internal.util.StringHelper; @@ -60,7 +58,6 @@ import org.hibernate.jpa.boot.spi.TypeContributorList; import org.hibernate.jpa.internal.util.LogHelper; import org.hibernate.jpa.internal.util.PersistenceUnitTransactionTypeHelper; -import org.hibernate.jpa.spi.IdentifierGeneratorStrategyProvider; import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode; import org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl; import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl; @@ -80,7 +77,6 @@ import io.quarkus.hibernate.orm.runtime.recording.PrevalidatedQuarkusMetadata; import io.quarkus.hibernate.orm.runtime.recording.RecordableBootstrap; import io.quarkus.hibernate.orm.runtime.recording.RecordedState; -import io.quarkus.hibernate.orm.runtime.service.QuarkusMutableIdentifierGeneratorFactory; import io.quarkus.hibernate.orm.runtime.service.QuarkusStaticInitDialectFactory; import io.quarkus.hibernate.orm.runtime.tenant.HibernateMultiTenantConnectionProvider; @@ -144,7 +140,6 @@ public FastBootMetadataBuilder(final QuarkusPersistenceUnitDefinition puDefiniti ssrBuilder.applySettings(buildTimeSettings.getAllSettings()); this.standardServiceRegistry = ssrBuilder.build(); - registerIdentifierGenerators(standardServiceRegistry); this.providedServices = ssrBuilder.getProvidedServices(); @@ -615,32 +610,6 @@ private static void applyTransactionProperties(PersistenceUnitDescriptor persist } } - private void registerIdentifierGenerators(StandardServiceRegistry ssr) { - final StrategySelector strategySelector = ssr.getService(StrategySelector.class); - - // apply id generators - final Object idGeneratorStrategyProviderSetting = buildTimeSettings - .get(AvailableSettings.IDENTIFIER_GENERATOR_STRATEGY_PROVIDER); - if (idGeneratorStrategyProviderSetting != null) { - final IdentifierGeneratorStrategyProvider idGeneratorStrategyProvider = strategySelector - .resolveStrategy(IdentifierGeneratorStrategyProvider.class, idGeneratorStrategyProviderSetting); - final IdentifierGeneratorFactory identifierGeneratorFactory = ssr - .getService(IdentifierGeneratorFactory.class); - if (identifierGeneratorFactory == null) { - throw persistenceException("Application requested custom identifier generator strategies, " - + "but the MutableIdentifierGeneratorFactory could not be found"); - } - if (!(identifierGeneratorFactory instanceof QuarkusMutableIdentifierGeneratorFactory)) { - throw persistenceException( - "Unexpected implementation of IdentifierGeneratorFactory: do not override core components"); - } - final QuarkusMutableIdentifierGeneratorFactory qIdGenerator = (QuarkusMutableIdentifierGeneratorFactory) identifierGeneratorFactory; - for (Map.Entry> entry : idGeneratorStrategyProvider.getStrategies().entrySet()) { - qIdGenerator.register(entry.getKey(), entry.getValue()); - } - } - } - /** * Greatly simplified copy of * org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl#populate(org.hibernate.boot.MetadataBuilder, diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/QuarkusMutableIdentifierGeneratorFactoryInitiator.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/QuarkusIdentifierGeneratorFactoryInitiator.java similarity index 66% rename from extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/QuarkusMutableIdentifierGeneratorFactoryInitiator.java rename to extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/QuarkusIdentifierGeneratorFactoryInitiator.java index 0a9c5a4d4e0a44..3e9ca583f83fb2 100644 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/QuarkusMutableIdentifierGeneratorFactoryInitiator.java +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/QuarkusIdentifierGeneratorFactoryInitiator.java @@ -4,22 +4,21 @@ import org.hibernate.boot.registry.StandardServiceInitiator; import org.hibernate.id.factory.IdentifierGeneratorFactory; +import org.hibernate.id.factory.internal.StandardIdentifierGeneratorFactory; import org.hibernate.service.spi.ServiceRegistryImplementor; /** - * We need to mimic the standard IdentifierGeneratorFactory but allowing - * to capture which Identifier strategies are being used, so that we can keep a reference to the classed - * needed at runtime. + * Uses a StandardIdentifierGeneratorFactory, but one that doesn't retrieve generators from CDI. * * @see IdentifierGeneratorFactory */ -public final class QuarkusMutableIdentifierGeneratorFactoryInitiator +public final class QuarkusIdentifierGeneratorFactoryInitiator implements StandardServiceInitiator { @Override public IdentifierGeneratorFactory initiateService(final Map configurationValues, final ServiceRegistryImplementor registry) { - return new QuarkusMutableIdentifierGeneratorFactory(registry); + return new StandardIdentifierGeneratorFactory(registry, true /* ignore bean container */); } @Override diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/QuarkusMutableIdentifierGeneratorFactory.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/QuarkusMutableIdentifierGeneratorFactory.java deleted file mode 100644 index 6ea478b7ee6e30..00000000000000 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/QuarkusMutableIdentifierGeneratorFactory.java +++ /dev/null @@ -1,218 +0,0 @@ -package io.quarkus.hibernate.orm.runtime.service; - -import static org.hibernate.cfg.AvailableSettings.IDENTIFIER_GENERATOR_STRATEGY_PROVIDER; -import static org.hibernate.id.factory.IdGenFactoryLogging.ID_GEN_FAC_LOGGER; - -import java.io.Serializable; -import java.util.Locale; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.ConcurrentHashMap; - -import jakarta.persistence.GenerationType; - -import org.hibernate.MappingException; -import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; -import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; -import org.hibernate.boot.registry.selector.spi.StrategySelector; -import org.hibernate.dialect.Dialect; -import org.hibernate.engine.config.spi.ConfigurationService; -import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; -import org.hibernate.generator.Generator; -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.SelectGenerator; -import org.hibernate.id.UUIDGenerator; -import org.hibernate.id.UUIDHexGenerator; -import org.hibernate.id.enhanced.SequenceStyleGenerator; -import org.hibernate.id.enhanced.TableGenerator; -import org.hibernate.id.factory.IdentifierGeneratorFactory; -import org.hibernate.id.factory.internal.AutoGenerationTypeStrategy; -import org.hibernate.id.factory.internal.IdentityGenerationTypeStrategy; -import org.hibernate.id.factory.internal.SequenceGenerationTypeStrategy; -import org.hibernate.id.factory.internal.TableGenerationTypeStrategy; -import org.hibernate.id.factory.internal.UUIDGenerationTypeStrategy; -import org.hibernate.id.factory.spi.GenerationTypeStrategy; -import org.hibernate.id.factory.spi.GenerationTypeStrategyRegistration; -import org.hibernate.id.factory.spi.GeneratorDefinitionResolver; -import org.hibernate.internal.log.DeprecationLogger; -import org.hibernate.jpa.spi.IdentifierGeneratorStrategyProvider; -import org.hibernate.service.ServiceRegistry; -import org.hibernate.type.Type; -import org.hibernate.type.descriptor.java.JavaType; - -/** - * Quarkus custom implementation of Hibernate ORM's org.hibernate.id.factory.internal.StandardIdentifierGeneratorFactory - * differences with the original: - * 1# it does not attempt to use a BeanContainer to create instances; Hibernate ORM introduced this feature in HHH-14688 - * 2# The register method is made public - * //TODO refactor ORM upstream so to allow code reuse - */ -public final class QuarkusMutableIdentifierGeneratorFactory - implements IdentifierGeneratorFactory, Serializable { - - private final ServiceRegistry serviceRegistry; - private final ConcurrentHashMap generatorTypeStrategyMap = new ConcurrentHashMap<>(); - private final ConcurrentHashMap> legacyGeneratorClassNameMap = new ConcurrentHashMap<>(); - - private Dialect dialect; - - public QuarkusMutableIdentifierGeneratorFactory(ServiceRegistry serviceRegistry) { - this.serviceRegistry = serviceRegistry; - registerJpaGenerators(); - logOverrides(); - registerPredefinedGenerators(); - registerUsingLegacyContributor(); - } - - //Same-as-upstream - private void registerJpaGenerators() { - generatorTypeStrategyMap.put(GenerationType.AUTO, AutoGenerationTypeStrategy.INSTANCE); - generatorTypeStrategyMap.put(GenerationType.SEQUENCE, SequenceGenerationTypeStrategy.INSTANCE); - generatorTypeStrategyMap.put(GenerationType.TABLE, TableGenerationTypeStrategy.INSTANCE); - generatorTypeStrategyMap.put(GenerationType.IDENTITY, IdentityGenerationTypeStrategy.INSTANCE); - generatorTypeStrategyMap.put(GenerationType.UUID, UUIDGenerationTypeStrategy.INSTANCE); - } - - private void logOverrides() { - serviceRegistry.getService(ClassLoaderService.class) - .loadJavaServices(GenerationTypeStrategyRegistration.class) - .forEach((registration) -> registration.registerStrategies( - (generationType, generationTypeStrategy) -> { - final GenerationTypeStrategy previous = generatorTypeStrategyMap.put(generationType, - generationTypeStrategy); - if (previous != null) { - ID_GEN_FAC_LOGGER.debugf( - "GenerationTypeStrategyRegistration [%s] overrode previous registration for GenerationType#%s : %s", - registration, - generationType.name(), - previous); - } - }, - serviceRegistry)); - } - - //Same-as-upstream - private void registerPredefinedGenerators() { - register("uuid2", UUIDGenerator.class); - // can be done with UuidGenerator + strategy - register("guid", GUIDGenerator.class); - register("uuid", UUIDHexGenerator.class); // "deprecated" for new use - register("uuid.hex", UUIDHexGenerator.class); // uuid.hex is deprecated - register("assigned", Assigned.class); - register("identity", IdentityGenerator.class); - register("select", SelectGenerator.class); - register("sequence", SequenceStyleGenerator.class); - register("increment", IncrementGenerator.class); - register("foreign", ForeignGenerator.class); - register("enhanced-sequence", SequenceStyleGenerator.class); - register("enhanced-table", TableGenerator.class); - } - - //Same-as-upstream - private void registerUsingLegacyContributor() { - final ConfigurationService configService = serviceRegistry.getService(ConfigurationService.class); - final Object providerSetting = configService.getSettings().get(IDENTIFIER_GENERATOR_STRATEGY_PROVIDER); - if (providerSetting != null) { - DeprecationLogger.DEPRECATION_LOGGER.deprecatedSetting2( - IDENTIFIER_GENERATOR_STRATEGY_PROVIDER, - "supply a org.hibernate.id.factory.spi.GenerationTypeStrategyRegistration Java service"); - final IdentifierGeneratorStrategyProvider idGeneratorStrategyProvider = serviceRegistry - .getService(StrategySelector.class) - .resolveStrategy(IdentifierGeneratorStrategyProvider.class, providerSetting); - for (Map.Entry> entry : idGeneratorStrategyProvider.getStrategies().entrySet()) { - @SuppressWarnings({ "rawtypes", "unchecked" }) - Class generatorClass = (Class) entry.getValue(); - register(entry.getKey(), generatorClass); - } - } - } - - //Same-as-upstream - but made public - public void register(String strategy, Class generatorClass) { - ID_GEN_FAC_LOGGER.debugf("Registering IdentifierGenerator strategy [%s] -> [%s]", strategy, generatorClass.getName()); - final Class previous = legacyGeneratorClassNameMap.put(strategy, generatorClass); - if (previous != null && ID_GEN_FAC_LOGGER.isDebugEnabled()) { - ID_GEN_FAC_LOGGER.debugf(" - overriding [%s]", previous.getName()); - } - } - - @Override //Same-as-upstream - public IdentifierGenerator createIdentifierGenerator( - GenerationType generationType, - String generatedValueGeneratorName, - String generatorName, - JavaType javaType, - Properties config, - GeneratorDefinitionResolver definitionResolver) { - final GenerationTypeStrategy strategy = generatorTypeStrategyMap.get(generationType); - if (strategy != null) { - return strategy.createIdentifierGenerator( - generationType, - generatorName, - javaType, - config, - definitionResolver, - serviceRegistry); - } - throw new UnsupportedOperationException("No GenerationTypeStrategy specified"); - } - - private Dialect getDialect() { //Same-as-upstream - if (dialect == null) { - dialect = serviceRegistry.getService(JdbcEnvironment.class).getDialect(); - } - return dialect; - } - - //Different than upstream: ignore all complexity related to it having generators - //managed by a bean container. - @Override - public Generator createIdentifierGenerator(String strategy, Type type, Properties parameters) { - final Class clazz = getIdentifierGeneratorClass(strategy); - try { - final Generator identifierGenerator = clazz.getConstructor().newInstance(); - if (identifierGenerator instanceof Configurable) { - ((Configurable) identifierGenerator).configure(type, parameters, serviceRegistry); - } - return identifierGenerator; - } catch (Exception e) { - final String entityName = parameters.getProperty(IdentifierGenerator.ENTITY_NAME); - throw new MappingException("Could not instantiate id generator [entity-name=" + entityName + "]"); - } - } - - private Class getIdentifierGeneratorClass(String strategy) { //Same-as-upstream - switch (strategy) { - case "hilo": - throw new UnsupportedOperationException("Support for 'hilo' generator has been removed"); - case "native": - strategy = getDialect().getNativeIdentifierGeneratorStrategy(); - //then fall through: - default: - Class generatorClass = legacyGeneratorClassNameMap.get(strategy); - return generatorClass != null ? generatorClass : generatorClassForName(strategy); - } - } - - private Class generatorClassForName(String strategy) { //Same-as-upstream - try { - Class clazz = serviceRegistry.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; - } catch (ClassLoadingException e) { - throw new MappingException(String.format(Locale.ROOT, "Could not interpret id generator strategy [%s]", strategy)); - } - } - -} diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/StandardHibernateORMInitiatorListProvider.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/StandardHibernateORMInitiatorListProvider.java index 04eaa9f9f032d7..c62e699c6bced7 100644 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/StandardHibernateORMInitiatorListProvider.java +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/service/StandardHibernateORMInitiatorListProvider.java @@ -82,8 +82,8 @@ public List> initialInitiatorList() { serviceInitiators.add(JdbcServicesInitiator.INSTANCE); serviceInitiators.add(RefCursorSupportInitiator.INSTANCE); - // Custom one! Also, this one has state so can't use the singleton. - serviceInitiators.add(new QuarkusMutableIdentifierGeneratorFactoryInitiator());// MutableIdentifierGeneratorFactoryInitiator.INSTANCE); + // Custom one! + serviceInitiators.add(new QuarkusIdentifierGeneratorFactoryInitiator()); serviceInitiators.add(QuarkusJtaPlatformInitiator.INSTANCE); diff --git a/extensions/hibernate-reactive/runtime/src/main/java/io/quarkus/hibernate/reactive/runtime/boot/registry/ReactiveHibernateInitiatorListProvider.java b/extensions/hibernate-reactive/runtime/src/main/java/io/quarkus/hibernate/reactive/runtime/boot/registry/ReactiveHibernateInitiatorListProvider.java index 169b52a90d19e9..be536c8c7b2781 100644 --- a/extensions/hibernate-reactive/runtime/src/main/java/io/quarkus/hibernate/reactive/runtime/boot/registry/ReactiveHibernateInitiatorListProvider.java +++ b/extensions/hibernate-reactive/runtime/src/main/java/io/quarkus/hibernate/reactive/runtime/boot/registry/ReactiveHibernateInitiatorListProvider.java @@ -33,8 +33,8 @@ import io.quarkus.hibernate.orm.runtime.customized.BootstrapOnlyProxyFactoryFactoryInitiator; import io.quarkus.hibernate.orm.runtime.customized.QuarkusJndiServiceInitiator; import io.quarkus.hibernate.orm.runtime.service.InitialInitiatorListProvider; +import io.quarkus.hibernate.orm.runtime.service.QuarkusIdentifierGeneratorFactoryInitiator; import io.quarkus.hibernate.orm.runtime.service.QuarkusImportSqlCommandExtractorInitiator; -import io.quarkus.hibernate.orm.runtime.service.QuarkusMutableIdentifierGeneratorFactoryInitiator; import io.quarkus.hibernate.orm.runtime.service.QuarkusRegionFactoryInitiator; import io.quarkus.hibernate.orm.runtime.service.QuarkusStaticInitDialectFactoryInitiator; import io.quarkus.hibernate.orm.runtime.service.StandardHibernateORMInitiatorListProvider; @@ -96,8 +96,8 @@ public List> initialInitiatorList() { serviceInitiators.add(JdbcServicesInitiator.INSTANCE); serviceInitiators.add(RefCursorSupportInitiator.INSTANCE); - // Custom one! Also, this one has state so can't use the singleton. - serviceInitiators.add(new QuarkusMutableIdentifierGeneratorFactoryInitiator());// MutableIdentifierGeneratorFactoryInitiator.INSTANCE); + // Custom one! + serviceInitiators.add(new QuarkusIdentifierGeneratorFactoryInitiator()); // Custom for Hibernate Reactive: serviceInitiators.add(NoJtaPlatformInitiator.INSTANCE);