org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0

Spring JIRA | Tim Yates | 4 years ago
tip
Do you know that we can give you better hits? Get more relevant results from Samebug’s stack trace search.
  1. 0

    *Bottom line*: When EntityManagerFactoryUtils.findEntityManagerFactory() is called with a persistence unit name, it looks for an EntityManagerFactory bean through the whole context hierarchy. If the persistence unit name is omitted, it will only look at the current context and ignore beans in any parent context. *Why this matters*: I have a Spring MVC application that has a global context in /WEB-INF/applicationContext.xml and an MVC context in /WEB-INF/dispatcher-servlet.xml. The EntityManagerFactory is configured in the global context, and we have an OpenEntityManagerInViewInterceptor in the MVC context. If I leave the OEMIVInterceptor's persistenceUnitName property undefined, the application fails to start up with this error: {noformat} org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0 at org.springframework.beans.factory.BeanFactoryUtils.beanOfType(BeanFactoryUtils.java:394) at org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:111) at org.springframework.orm.jpa.EntityManagerFactoryAccessor.setBeanFactory(EntityManagerFactoryAccessor.java:139) at ... {noformat} However, if I specify the persistenceUnitName, all is well. This also worked previously when all the beans were defined in one context (without the need to specify the persistence unit). If this is not a bug, I fail to see the logic behind it. Here is the relevant snippet from EntityManagerFactoryUtils.findEntityManagerFactory(): {code} if (StringUtils.hasLength(unitName)) { // See whether we can find an EntityManagerFactory with matching persistence unit name. String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, EntityManagerFactory.class); // ... } else { return BeanFactoryUtils.beanOfType(beanFactory, EntityManagerFactory.class); } {code} As you can see, it calls the "...IncludingAncestors" method if the persistence unit name is defined, but uses the no-hierarchy version otherwise. This could easily be fixed by changing the second call to: {code} else { return BeanFactoryUtils.beanOfTypeIncludingAncestors(beanFactory, EntityManagerFactory.class); } {code}

    Spring JIRA | 4 years ago | Tim Yates
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0
  2. 0

    *Bottom line*: When EntityManagerFactoryUtils.findEntityManagerFactory() is called with a persistence unit name, it looks for an EntityManagerFactory bean through the whole context hierarchy. If the persistence unit name is omitted, it will only look at the current context and ignore beans in any parent context. *Why this matters*: I have a Spring MVC application that has a global context in /WEB-INF/applicationContext.xml and an MVC context in /WEB-INF/dispatcher-servlet.xml. The EntityManagerFactory is configured in the global context, and we have an OpenEntityManagerInViewInterceptor in the MVC context. If I leave the OEMIVInterceptor's persistenceUnitName property undefined, the application fails to start up with this error: {noformat} org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0 at org.springframework.beans.factory.BeanFactoryUtils.beanOfType(BeanFactoryUtils.java:394) at org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:111) at org.springframework.orm.jpa.EntityManagerFactoryAccessor.setBeanFactory(EntityManagerFactoryAccessor.java:139) at ... {noformat} However, if I specify the persistenceUnitName, all is well. This also worked previously when all the beans were defined in one context (without the need to specify the persistence unit). If this is not a bug, I fail to see the logic behind it. Here is the relevant snippet from EntityManagerFactoryUtils.findEntityManagerFactory(): {code} if (StringUtils.hasLength(unitName)) { // See whether we can find an EntityManagerFactory with matching persistence unit name. String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, EntityManagerFactory.class); // ... } else { return BeanFactoryUtils.beanOfType(beanFactory, EntityManagerFactory.class); } {code} As you can see, it calls the "...IncludingAncestors" method if the persistence unit name is defined, but uses the no-hierarchy version otherwise. This could easily be fixed by changing the second call to: {code} else { return BeanFactoryUtils.beanOfTypeIncludingAncestors(beanFactory, EntityManagerFactory.class); } {code}

    Spring JIRA | 4 years ago | Tim Yates
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0
  3. 0

    After updating Spring to 3.1.3.RELEASE application does not start

    GitHub | 4 years ago | kolorobot
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.github.kolorobot.config.PersistenceConfig': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'annotationDrivenTransactionManager' defined in class path resource [com/github/kolorobot/config/PersistenceConfig.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    JavaConfig: EntityManagerFactory not found by OpenEntityManagerInViewFilter

    Stack Overflow | 4 years ago | user871611
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0
  6. 0

    NoSuchBeanDefinitionException when using spring mvc with JPA

    Stack Overflow | 3 weeks ago | tornadoKatcher
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined

    Root Cause Analysis

    1. org.springframework.beans.factory.NoSuchBeanDefinitionException

      No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0

      at org.springframework.beans.factory.BeanFactoryUtils.beanOfType()
    2. Spring Beans
      BeanFactoryUtils.beanOfType
      1. org.springframework.beans.factory.BeanFactoryUtils.beanOfType(BeanFactoryUtils.java:394)
      1 frame
    3. Spring ORM
      EntityManagerFactoryAccessor.setBeanFactory
      1. org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:111)
      2. org.springframework.orm.jpa.EntityManagerFactoryAccessor.setBeanFactory(EntityManagerFactoryAccessor.java:139)
      2 frames