java.lang.IllegalArgumentException:在EntityManagerFactory配置中没有指定PersistenceProvider

med*_*bli 9 maven applicationcontext postgresql-9.1 hibernate-4.x spring-4

我创建了一个maven项目并通过pom.xml 添加了Spring4,Hibernate4库我试图将我的web应用程序与我在PostgreSql中创建的数据库链接,但是当我在apache tomcat 7中发布我的项目时,会发生以下异常:

org.springframework.beans.factory.BeanCreationException:在ServletContext资源[/WEB-INF/applicationContext.xml]中定义名为'emf'的bean时出错:init方法的调用失败; 嵌套异常是java.lang.IllegalArgumentException:在EntityManagerFactory配置中没有指定PersistenceProvider,并且选择的PersistenceUnitInfo没有在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)的org中指定提供者类名.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:

这是我的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


    <context:component-scan base-package="com.medsoft.stadto">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:1993/Posts" />
        <property name="username" value="postgres" />
        <property name="password" value="123" />
    </bean>

    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="emf">
        <property name="packagesToScan" value="com.medsoft.stadto.entity" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2dll.auto">create</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="txManager" />
    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>
Run Code Online (Sandbox Code Playgroud)

Mar*_*tör 13

更改emfbean配置并添加一个名为的新bean jpaVendorAdapter:

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="emf">
  <property name="packagesToScan" value="com.medsoft.stadto.entity" />
  <property name="dataSource" ref="dataSource" />
  <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
  <property name="persistenceUnitName" value="stadto"/>
  <property name="jpaProperties">
    <props>
      <prop key="hibernate.show_sql">true</prop>
      <prop key="hibernate.hbm2ddl.auto">create</prop>
    </props>
  </property>
</bean>

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
  <property name="showSql" value="true"/>
  <property name="generateDdl" value="true"/>
  <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

还要确保您persistence.xmlMETA-INF目录中有一个:

<?xml version="1.0"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
  version="2.1">
  <persistence-unit name="stadto">
    //No need to specify the provider as we already have specified JPA vendor in applicationContext.xml
  </persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)

  • 有一种方法可以不使用persistence.xml。您可以使用休眠的jpa供应商适配器创建LocalContainerEntityManagerFactoryBean;@Bean public LocalContainerEntityManagerFactoryBean directoryManagerFactory(){HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); 返回localContainerEntityManagerFactoryBean;} (2认同)

Moh*_*ima 5

当您没有为 JPA 配置 JpaVendorImplementation 时会发生这种情况。JPA EntityManager 的以下 Java bean 定义对我有用:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    emf.setDataSource(dataSource);
    emf.setPackagesToScan(irdbConfig.getPathForPackagesToScan());
    emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    emf.setJpaPropertyMap(irdbConfig.getAdditionalHibernateProperties());
    return emf;
}
Run Code Online (Sandbox Code Playgroud)

但是下面的一个抛出与您收到的相同的异常:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    emf.setDataSource(dataSource);
    emf.setPackagesToScan(irdbConfig.getPathForPackagesToScan());        
    return emf;
}
Run Code Online (Sandbox Code Playgroud)

请注意从第二个代码中删除的行。