HHH-15068 Don't try to instantiate types through the bean manager when that's not possible

This commit is contained in:
Yoann Rodière 2022-02-03 13:54:36 +01:00 committed by Christian Beikov
parent b8060d2df7
commit 51e556ebde
3 changed files with 52 additions and 31 deletions

View File

@ -16,12 +16,14 @@ import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import org.hibernate.boot.model.process.internal.UserTypeResolution;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.mapping.BasicValue;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.model.convert.spi.BasicValueConverter;
import org.hibernate.resource.beans.internal.Helper;
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
import org.hibernate.resource.beans.spi.ManagedBean;
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
@ -144,21 +146,8 @@ public class TypeDefinition implements Serializable {
// support for AttributeConverter would be nice too
if ( isKnownType ) {
final ManagedBean typeBean;
if ( name != null ) {
typeBean = bootstrapContext
.getServiceRegistry()
.getService( ManagedBeanRegistry.class )
.getBean( name, typeImplementorClass, instanceProducer );
}
else {
typeBean = bootstrapContext
.getServiceRegistry()
.getService( ManagedBeanRegistry.class )
.getBean( typeImplementorClass, instanceProducer );
}
final Object typeInstance = typeBean.getBeanInstance();
final Object typeInstance = instantiateType( bootstrapContext.getServiceRegistry(),
name, typeImplementorClass, instanceProducer );
if ( typeInstance instanceof TypeConfigurationAware ) {
( (TypeConfigurationAware) typeInstance ).setTypeConfiguration( typeConfiguration );
@ -288,6 +277,32 @@ public class TypeDefinition implements Serializable {
);
}
private static Object instantiateType(StandardServiceRegistry serviceRegistry,
String name, Class<?> typeImplementorClass,
BeanInstanceProducer instanceProducer) {
if ( Helper.INSTANCE.shouldIgnoreBeanContainer( serviceRegistry ) ) {
if ( name != null ) {
return instanceProducer.produceBeanInstance( name, typeImplementorClass );
}
else {
return instanceProducer.produceBeanInstance( typeImplementorClass );
}
}
else {
final ManagedBean typeBean;
if ( name != null ) {
typeBean = serviceRegistry.getService( ManagedBeanRegistry.class )
.getBean( name, typeImplementorClass, instanceProducer );
}
else {
typeBean = serviceRegistry.getService( ManagedBeanRegistry.class )
.getBean( typeImplementorClass, instanceProducer );
}
return typeBean.getBeanInstance();
}
}
public static BasicValue.Resolution<?> createLocalResolution(
String name,
Class typeImplementorClass,

View File

@ -45,6 +45,7 @@ import org.hibernate.resource.beans.container.spi.BeanContainer;
import org.hibernate.resource.beans.container.spi.ContainedBean;
import org.hibernate.resource.beans.container.spi.ExtendedBeanManager;
import org.hibernate.resource.beans.internal.FallbackBeanInstanceProducer;
import org.hibernate.resource.beans.internal.Helper;
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;
@ -76,22 +77,7 @@ public class StandardIdentifierGeneratorFactory
* Constructs a new factory
*/
public StandardIdentifierGeneratorFactory(ServiceRegistry serviceRegistry) {
this( serviceRegistry, shouldIgnoreBeanContainer( serviceRegistry ) );
}
private static boolean shouldIgnoreBeanContainer(ServiceRegistry serviceRegistry) {
final ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class );
final Object beanManagerRef = configService.getSettings().get( AvailableSettings.JAKARTA_CDI_BEAN_MANAGER );
if ( beanManagerRef instanceof ExtendedBeanManager ) {
return true;
}
if ( configService.getSetting( AvailableSettings.DELAY_CDI_ACCESS, StandardConverters.BOOLEAN, false ) ) {
return true;
}
return false;
this( serviceRegistry, Helper.INSTANCE.shouldIgnoreBeanContainer( serviceRegistry ) );
}
/**

View File

@ -6,9 +6,14 @@
*/
package org.hibernate.resource.beans.internal;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.resource.beans.container.spi.BeanLifecycleStrategy;
import org.hibernate.resource.beans.container.internal.ContainerManagedLifecycleStrategy;
import org.hibernate.resource.beans.container.internal.JpaCompliantLifecycleStrategy;
import org.hibernate.resource.beans.container.spi.ExtendedBeanManager;
import org.hibernate.service.ServiceRegistry;
/**
* @author Steve Ebersole
@ -30,6 +35,21 @@ public class Helper {
return beanType.getName() + ':' + name;
}
public boolean shouldIgnoreBeanContainer(ServiceRegistry serviceRegistry) {
final ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class );
final Object beanManagerRef = configService.getSettings().get( AvailableSettings.JAKARTA_CDI_BEAN_MANAGER );
if ( beanManagerRef instanceof ExtendedBeanManager ) {
return true;
}
if ( configService.getSetting( AvailableSettings.DELAY_CDI_ACCESS, StandardConverters.BOOLEAN, false ) ) {
return true;
}
return false;
}
@SuppressWarnings("unused")
public BeanLifecycleStrategy getLifecycleStrategy(boolean shouldRegistryManageLifecycle) {
return shouldRegistryManageLifecycle