user871611
user871611

Reputation: 3462

JavaConfig: EntityManagerFactory not found by OpenEntityManagerInViewFilter

I'm trying to get my spring-application xml-free using Spring JavaConfig.

Now I'm facing the following error requesting a web-page:

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.support.OpenEntityManagerInViewFilter.lookupEntityManagerFactory(OpenEntityManagerInViewFilter.java:229)
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.lookupEntityManagerFactory(OpenEntityManagerInViewFilter.java:205)
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:152)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

My persistence-configuration:

@Configuration
@EnableTransactionManagement
public class PersistenceConfig implements TransactionManagementConfigurer
{

  @Bean(name = "dataSource")
  public DataSource dataSource()
  {
    try
    {
      return (DataSource) new JndiTemplate().lookup("java:comp/env/jdbc/my-db");
    }
    catch (NamingException e)
    {
      throw new ApplicationConfigurationException("db-context not found", e);
    }
  }

  @Bean(name = "entityManagerFactory")
  public EntityManagerFactory entityManagerFactory()
  {
    LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean();
    lcemfb.setDataSource(dataSource());
    lcemfb.setJpaDialect(new HibernateJpaDialect());
    lcemfb.setJpaVendorAdapter(jpaVendorAdapter());
    lcemfb.setPersistenceUnitName("persistenceUnit");
    lcemfb.setPersistenceXmlLocation("classpath:META-INF/persistence.xml");
    lcemfb.afterPropertiesSet();
    return lcemfb.getObject();
  }

  @Bean(name = "jpaVendorAdapter")
  public JpaVendorAdapter jpaVendorAdapter()
  {
    HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setShowSql(true);
    jpaVendorAdapter.setDatabase(Database.ORACLE);
    jpaVendorAdapter.setDatabasePlatform(Oracle10gDialect.class.getName());
    jpaVendorAdapter.setGenerateDdl(false);
    return jpaVendorAdapter;
  }

  /**
   * @see org.springframework.transaction.annotation.TransactionManagementConfigurer#annotationDrivenTransactionManager()
   */
  @Bean(name = "transactionManager")
  public PlatformTransactionManager annotationDrivenTransactionManager()
  {
    JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
    jpaTransactionManager.setEntityManagerFactory(entityManagerFactory());
    return jpaTransactionManager;
  }
}

Database-Access within the application is working fine so far.

Any hints?

Upvotes: 1

Views: 4719

Answers (1)

Lee Meador
Lee Meador

Reputation: 12985

You may need to register the container or do the scan. See here (Click me)

Upvotes: 1

Related Questions