HHH-14914 - Passing an ExtendedBeanManager which is never notified leads to runtime errors

This commit is contained in:
Steve Ebersole 2021-11-04 15:49:11 -05:00
parent 0ea110aea2
commit 9fec060fe2
2 changed files with 83 additions and 0 deletions

View File

@ -104,6 +104,21 @@ public void initialize() {
return;
}
if ( beanManager == null ) {
try {
beanInstance = fallbackProducer.produceBeanInstance( beanType );
return;
}
catch (Exception e) {
// the CDI BeanManager is not know to be ready for use and the
// fallback-producer was unable to create the bean...
throw new IllegalStateException(
"CDI BeanManager is not known to be ready for use and the fallback-producer was unable to create the bean",
new NotYetReadyException( e )
);
}
}
final AnnotatedType<B> annotatedType;
try {
annotatedType = beanManager.createAnnotatedType( beanType );

View File

@ -0,0 +1,68 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.cdi.lifecycle;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.resource.beans.container.spi.ExtendedBeanManager;
import org.junit.jupiter.api.Test;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
/**
* We pass an ExtendedBeanManager but never "initialize" it. This might happen when the
* environment provides an ExtendedBeanManager but there is no CDI needed for the app
*/
public class ExtendedBeanManagerNoCallbackTest {
@Test
public void tryIt() {
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
.applySetting( AvailableSettings.JAKARTA_CDI_BEAN_MANAGER, new ExtendedBeanManagerImpl() )
.build();
try {
// this will trigger trying to locate IdentifierGeneratorFactory as a managed-bean
new MetadataSources( ssr )
.addAnnotatedClass( TheEntity.class )
.buildMetadata()
.buildSessionFactory();
}
finally {
StandardServiceRegistryBuilder.destroy( ssr );
}
}
@Entity(name = "TheEntity")
@Table(name = "TheEntity")
public static class TheEntity {
@Id
@GeneratedValue
private Integer id;
private String name;
}
public static class ExtendedBeanManagerImpl implements ExtendedBeanManager {
private LifecycleListener lifecycleListener;
@Override
public void registerLifecycleListener(LifecycleListener lifecycleListener) {
assert this.lifecycleListener == null;
this.lifecycleListener = lifecycleListener;
}
public void notify(BeanManager ready) {
lifecycleListener.beanManagerInitialized( ready );
}
}
}