首页 文章

JPA没有定义[javax.persistence.EntityManagerFactory]类型的限定bean

提问于
浏览
1

我在Tomcat 8上部署了一个JPA / spring / hibernate应用程序 . 但是当tomcat服务器启动时我可以看到这个警告消息“没有定义类型为[javax.persistence.EntityManagerFactory]的限定bean” . 将不胜感激,因为我不知道配置错误 .

persistence.xml中

<persistence-unit name="persistenceUnit"
    transaction-type="RESOURCE_LOCAL">
...

spring上下文文件被定义为多个文件以便重用

上下文ds.xml中

<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="datasource" />
    <property name="persistenceUnitName" value="persistenceUnit" />
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManager" />
</bean>
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
...

背景-config.xml中

<context:annotation-config />
<context:component-scan base-package="com.app" />
<jpa:repositories base-package="com.app" />
<tx:annotation-driven transaction-manager="txManager"
    order="200" />

和web.xml

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/context-config.xml, /WEB-INF/context-ds.xml, /WEB-INF/context-dao.xml</param-value>
</context-param>

和DAO抽象类定义PersistenceContext注释

@PersistenceContext
protected EntityManager entityManager;

真实的消息

11:45:18.710 [localhost-startStop-1] WARN  AbstractBeanFactory.getTypeForFactoryBean - Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDAO' defined in ServletContext resource [/WEB-INF/context-dao.xml]: Cannot create inner bean 'genericDao$child#632cb33' of type [com.app.dao.GenericDaoImpl] while setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'genericDao$child#632cb33': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined

如果我像这样声明PersistenceContext,我会得到消息没有定义名为'persistenceUnit'的bean

@PersistenceContext(unitName="persistenceUnit")
protected EntityManager entityManager;

1 回答

  • 0

    你必须在一个@Configuration注释类中定义一个这样的bean ......并根据你的引用命名它

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    
        LocalContainerEntityManagerFactoryBean factory = null;
        try {
            HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
            vendorAdapter.setGenerateDdl(true);
            vendorAdapter.setShowSql(true);
    
            vendorAdapter.setDatabasePlatform(MyAppSettings.getDbPlattform());
    
            HibernateJpaDialect jpd = new HibernateJpaDialect();
            factory = new LocalContainerEntityManagerFactoryBean();
    
            factory.setJpaDialect(jpd);
            factory.setJpaVendorAdapter(vendorAdapter);
            factory.setPackagesToScan(MyAppSettings.packagesToScan);
            factory.setDataSource(MyDataSource());
    
        } catch (SQLException e) {
            e.printStackTrace();
        }
    
        return factory;
    }
    

    编辑:

    也许你的spring config xml不包含在组件扫描中尝试:

    @ImportResource("classpath:spring-config.xml")
    

    ....请发布你的完整stacjtrace ...

相关问题