diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringBeanContainer.java b/spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringBeanContainer.java index 6e73747a5929..2a4ac9e08453 100644 --- a/spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringBeanContainer.java +++ b/spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringBeanContainer.java @@ -24,6 +24,7 @@ import org.hibernate.resource.beans.container.spi.BeanContainer; import org.hibernate.resource.beans.container.spi.ContainedBean; import org.hibernate.resource.beans.spi.BeanInstanceProducer; +import org.hibernate.type.spi.TypeBootstrapContext; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanCreationException; @@ -180,14 +181,28 @@ private SpringContainedBean createBean( try { if (lifecycleOptions.useJpaCompliantCreation()) { + Object bean = null; + if (fallbackProducer instanceof TypeBootstrapContext) { + // Special Hibernate type construction rules, including TypeBootstrapContext resolution. + bean = fallbackProducer.produceBeanInstance(name, beanType); + } if (this.beanFactory.containsBean(name)) { - Object bean = this.beanFactory.autowire(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); + if (bean == null) { + bean = this.beanFactory.autowire(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); + } this.beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false); this.beanFactory.applyBeanPropertyValues(bean, name); bean = this.beanFactory.initializeBean(bean, name); return new SpringContainedBean<>(bean, beanInstance -> this.beanFactory.destroyBean(name, beanInstance)); } + else if (bean != null) { + // No bean found by name but constructed with TypeBootstrapContext rules + this.beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false); + bean = this.beanFactory.initializeBean(bean, name); + return new SpringContainedBean<>(bean, this.beanFactory::destroyBean); + } else { + // No bean found by name -> construct by type using createBean return new SpringContainedBean<>( // to be replaced with plain createBean(Class) this.beanFactory.createBean(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false), this.beanFactory::destroyBean);