BeanManager createInstance being called before AfterBeanDiscovery event fired


We are using Hibernate ORM as the JPA persistence provider in the WebSphere Application Server (Libertry), along with Hibernate Search. WebSphere Liberty CDI 2.0 is based on Weld.

During startup of the container, Hibernate Search tries to use the BeanManager prematurely, before the container has fired the AfterBeanDiscovery event, in relation to creating FieldBridge instances.

This behavior is in violation of the Java EE 8 Specification for the BeanManager interface, which states that the createInstance method must not be called before AfterBeanDiscovery event is fired.

Here is the stack trace: HSEARCH000139: Unable to instantiate FieldBridge
at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(
at org.hibernate.internal.SessionFactoryImpl.(

Caused by: org.hibernate.resource.beans.container.internal.NotYetReadyException: CDI BeanManager not (yet) ready to use
resolveContainerInstance:160, ContainerManagedLifecycleStrategy$BeanImpl (org.hibernate.resource.beans.container.internal)
initialize:92, ContainerManagedLifecycleStrategy$AbstractBeanImpl (org.hibernate.resource.beans.container.internal)
createBean:43, CdiBeanContainerImmediateAccessImpl (org.hibernate.resource.beans.container.internal)
createBean:64, AbstractCdiBeanContainer (org.hibernate.resource.beans.container.spi)
getBean:38, AbstractCdiBeanContainer (org.hibernate.resource.beans.container.spi)
resolve:63, HibernateOrmBeanContainerBeanResolver (
resolve:40, ReflectionFallbackBeanResolver (
createFieldBridgeFromAnnotation:371, BridgeFactory (
findExplicitFieldBridge:350, BridgeFactory (
buildFieldBridge:203, BridgeFactory (
bindFieldAnnotation:1335, AnnotationMetadataProvider (
checkForField:1258, AnnotationMetadataProvider (
initializeMemberLevelAnnotations:1065, AnnotationMetadataProvider (
initializeClass:599, AnnotationMetadataProvider (
doGetTypeMetadataFor:192, AnnotationMetadataProvider (
getTypeMetadataFor:181, AnnotationMetadataProvider (
getTypeMetadataFor:165, AnnotationMetadataProvider (
initDocumentBuilders:445, SearchIntegratorBuilder (
createNewFactoryState:244, SearchIntegratorBuilder (
buildNewSearchFactory:200, SearchIntegratorBuilder (
buildSearchIntegrator:128, SearchIntegratorBuilder (
boot:127, HibernateSearchSessionFactoryObserver (
sessionFactoryCreated:94, HibernateSearchSessionFactoryObserver (
sessionFactoryCreated:35, SessionFactoryObserverChain (org.hibernate.internal)
:371, SessionFactoryImpl (org.hibernate.internal)
build:462, SessionFactoryBuilderImpl (org.hibernate.boot.internal)
build:938, EntityManagerFactoryBuilderImpl (org.hibernate.jpa.boot.internal)
createContainerEntityManagerFactory:141, HibernatePersistenceProvider (org.hibernate.jpa)
createEMFactory:910, JPAPUnitInfo (
initialize:762, JPAPUnitInfo (
extractPersistenceUnits:183, JPAPxmlInfo (
processPersistenceUnit:89, JPAScopeInfo (
addPersistenceUnits:119, JPAApplInfo (
processEJBModulePersistenceXml:543, JPAComponentImpl (
applicationStarting:275, JPAComponentImpl (
fireStarting:28, ApplicationStateManager (
fireApplicationStarting:50, StateChangeServiceImpl (
preDeployApp:383, DeployedAppInfoBase (
deployApp:412, DeployedAppInfoBase (
install:76, EARApplicationHandlerImpl (
execute:140, StartAction (
enterState:1258, ApplicationStateMachineImpl (
run:873, ApplicationStateMachineImpl (
run:239, ExecutorServiceImpl$RunnableWrapper (
runWorker:1142, ThreadPoolExecutor (java.util.concurrent)
run:617, ThreadPoolExecutor$Worker (java.util.concurrent)
run:745, Thread (java.lang)
Caused by: java.lang.IllegalStateException: java.lang.UnsupportedOperationException: No current bean manager found in CDI service
at com.sun.proxy.$Proxy65.createInstance(Unknown Source)
at org.hibernate.resource.beans.container.internal.ContainerManagedLifecycleStrategy$BeanImpl.resolveContainerInstance(
… 49 more
Caused by: java.lang.UnsupportedOperationException: No current bean manager found in CDI service
… 51 more



This is most likely a problem with how CDI is integrated with Hibernate ORM in Websphere.

You see, Hibernate Search does not have a choice about when it creates beans; it must create beans when it starts, and it starts when Hibernate ORM tells it to. Hibernate Search also doesn’t have a clue about the dependency injection technology being used under the hood (CDI, Spring, etc.): it just uses SPIs provided by Hibernate ORM.

In order for Hibernate Search to delay its start, it would need the CDI engine to send it an event at the appropriate time. Fortunately, there’s a mechanism just for that in the SPIs allowing to integrate a CDI engine into Hibernate ORM, and it already works in WildFly. However, in order for this mechanism to work, the application server must pass to Hibernate ORM a bean manager that implements a specific interface: org.hibernate.resource.beans.container.spi.ExtendedBeanManager.

If Hibernate ORM is built into Websphere, I would suggest you submit a ticket to them, requesting that the bean manager passed to Hibernate ORM through the javax.persistence.bean.manager property implements org.hibernate.resource.beans.container.spi.ExtendedBeanManager. They don’t have to implement the interface directly in their own CDI solution, they could just wrap the bean manager in another object that implements that interface, just like WildFly did.
See the javadoc of org.hibernate.cfg.AvailableSettings#CDI_BEAN_MANAGER for more information.

If Hibernate ORM is not built into Websphere, or if you need a quick hack to solve the problem in your application, you can always implement the “notification” mechanism from the CDI engine to Hibernate Search yourself. You will need to do three things:

  • Implement a
  • Make sure the synchronizer is made available to Hibernate Search through Hibernate ORM by implementing a org.hibernate.service.spi.ServiceContributor. You can find an example implementation here. Your implementation will be different, but that will give you an idea of what you can do.
  • Make sure the service contributor is detected by Hibernate ORM by adding a file named META-INF/services/org.hibernate.service.spi.ServiceContributor to your resources, and setting its content to a single line of text: the fully-qualified name of your service contributor class.


Thank you very much for your reply. The problem has been solved successfully, after registering a CDI Extension with the container via META-INF/services/javax.enterprise.inject.spi.Extension to listen to the bean discovery events, and then wiring up the EnvironmentSynchronizer as you describe.

I’m not sure how much traction I will be able to achive with IBM if I ask them to make changes to their implementation of the spec, when we already have a way to simply abide by it… If Hibernate uses the BeanManager interface, shouldn’t it play by the rules, without any extra remediation or accommodation?

I am asking, how do I make the case to IBM that Hibernate is above the law? This document, which Gavin King wrote (among others), is very clear about when not to make calls to the BeanManager:

After reading this, it does seem like Hibernate is the proper place to listen to the bean discovery events from the container, since Hibernate is the one making use of the BeanManager API, both of which are constituents of the injection framework.

If there is a case to be made for doing this within the container, I’d like to understand that point of view so I can write a convincing case to IBM.

Thank you again, this formula is exactly what I needed to get our application up and running on the latest releases of Hibernate, Hibernate Search, and WebSphere Liberty.



From what I understand, the extension was necessary in order to make things work; for some reason the CDI spec didn’t allow us to do what we wanted exactly.

@sebersole might be able to tell you more, I personally don’t really remember the details of why EntityManager had to be extended.

What I know is that, in the early days of the Hibernate Search/CDI experiments (when we were not relying on ORM to provide the CDI integration), Hibernate Search used to rely on a simple hack. It got more complicated in WildFly 11, and we started relying on the ExtendedBeanManager interface that was provided by Wildfly, but I think the idea stayed the same. Then ORM started to provide its own CDI integration and we switched to that in Hibernate Search.

Maybe that hack, or a cleaner solution, could be implemented in Hibernate ORM? If you want to give it a try, we certainly could have a look at a PR. I think @sebersole would be glad to get rid of ExtendedBeanManager too, if the alternative works just as well.



It is a chicken-egg problem that is actually caused by the EE spec and its various sub-specs. At the most basic:

  1. CDI wants JPA initialized before it finishes its own initialization so that it can inject EntityManagerFactory references. This is according to the CDI spec.
  2. JPA wants CDI initialized so that it can consume CDI beans as “entity listeners”.

The EE spec, unfortunately, does nothing to dictate how this should work.

Now technically speaking the JPA spec does not explicitly say that CDI should be accessed on JPA bootstrap. To that end Hibernate does offer 2 additional options specifically aimed at alleviating this chicken-egg problem:

  1. Instead of passing an instance the container’s BeanManager impl (via the standard javax.persistence.bean.manager setting), pass an instance of org.hibernate.resource.beans.container.spi.ExtendedBeanManager.
  2. Set hibernate.delay_cdi_access to true.

Option (1) is, I think, what @yrodiere calls “a hack”. But really we do not consider it a hack per-se. We consider it a solution to the inherent problem in EE. In fact we (Red Hat) have requested something similar in all the EE-related specs. Really though, this option is meant more as a container-level concern. However, it has significant pros compared to…

Option (2) essentially tells Hibernate to delay accessing the BeanManager until it first needs to which is during runtime. In other words, the first time you perform an operation that needs a particular CDI bean Hibernate will ask the BeanManager for it. This has a serious downside in that if the bean does not exist you will not know about that until runtime, conceivably months after a deployment.

Because option (2) delays config/bootstrap errors until runtime, we actually consider this approach the hack. But generally speaking it is the easiest for users to use.

FWIW… CDI does have some form of “BeanManager is ready to use” callback already, but to be honest it is sh*t and does not work for cases like this. It is statically defined with no way to associate the triggered event back to the SessionFactory / EntityManagerFactory - we’d have to time travel back to the 1980’s and use thread-locals or some such to use that approach.




Steve, thank you for your comments, that sheds quite a little light on the subject.

In our CDI extension, the javax.enterprise.inject.spi.AfterBeanDiscovery event is working well as the trigger to proceed with the boot(sessionFactory) process of Hibernate Search. This event comes before AfterDeploymentValidation.

Since IBM now owns RedHat, I have been wondering if EclipseLink will be replaced by Hibernate any time soon, as the default persistence provider of choice. It is also interesting to have Wildfly, WebSphere and WebSphere Liberty under the same roof so to speak.

According to one of the WebSphere/Liberty developers in Rochester, it was IBM who contributed the WebSphereLibertyJtaPlatform to Hibernate, so at least I can see the integration is on their mind, and they are willing to make things easier.

We do have a WebSphere support contract, and I plan to open a case at IBM per our conversation. Thank you, your help is much appreciated.



Good news! IBM has opened an APAR: PH10946 to address this issue, and the IBM CDI L3 team is building the iFix. When the changes are available, I’ll update this thread.