maven java.lang.IllegalArgumentException:EntityManagerFactory配置中未指定PersistenceProvider

0g0grzrc  于 2023-06-29  发布在  Maven
关注(0)|答案(3)|浏览(107)

我创建了一个maven项目,并通过pom.xml添加了Spring4,Hibernate4libs我尝试将我的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豆子工厂。支持。AbstractAutowireCapableBeanFactory. initializeBean(AbstractAutowireCapableBeanFactory.java:1553)at org. SpringFramework豆子工厂。支持。AbstractAutowireCapableBeanFactory. doCreateBean(AbstractAutowireCapableBeanFactory.java:539)at org. SpringFramework豆子工厂。支持。AbstractAutowireCapableBeanFactory. createBean(AbstractAutowireCapableBeanFactory.java:475)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)at org. SpringFramework豆子工厂。支持。DefaultSingletonBeanRegistry。getSingleton(DefaultSingletonBeanRegistry.java:228)at org. SpringFramework豆子工厂。支持。AbstractBeanFactory. org上的doGetBean(AbstractBeanFactory.java:300)。SpringFramework豆子工厂。支持。AbstractBeanFactory. getBean(AbstractBeanFactory.java:195)at org. SpringFramework上下文支持。AbstractApplicationContext。getBean(AbstractApplicationContext.java:973)at org. SpringFramework上下文支持。AbstractApplicationContext。finishBeanFactoryInitialization(AbstractApplicationContext.java:750)at org. SpringFramework上下文支持。AbstractApplicationContext。在org上刷新(AbstractApplicationContext.java:482)。SpringFramework web. context. ContextLoader。org上的configureAndRefreshWebApplicationContext(ContextLoader.java:403)。SpringFramework web. context. ContextLoader。org上的initWebApplicationContext(ContextLoader.java:306)。SpringFramework web. context. ContextLoaderListener。org上的contextInitialized(ContextLoaderListener.java:106)。Apachecatalina核心StandardContext。listenerStart(StandardContext.java:4994)at org.Apachecatalina核心StandardContext。startInternal(StandardContext.java:5492)at org.Apachecatalina效用LifecycleBase. start(LifecycleBase.java:150)at org.Apachecatalina核心ContainerBase $ www.example.com EntityManagerFactory配置中未指定PersistenceProvider,并且所选PersistenceUnitInfo在org中也未指定提供程序类名。SpringFramework orm. LocalContainerEntityManagerFactoryBean。createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:318)at org. SpringFramework orm. AbstractEntityManagerFactoryBean。afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)at org. SpringFramework豆子工厂。支持。AbstractAutowireCapableBeanFactory. invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)at org. SpringFramework豆子工厂。支持。AbstractAutowireCapableBeanFactory. initializeBean(AbstractAutowireCapableBeanFactory.java:1549)...StartChild.call
下面是我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>
fcy6dtqo

fcy6dtqo1#

更改emf bean配置并添加一个名为jpaVendorAdapter的新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="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>

另外,请确保在META-INF目录中有一个persistence.xml

<?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>
sdnqo3pr

sdnqo3pr2#

如果您没有为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;
}

但下面的一个抛出了你收到的相同的异常:

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

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

yjghlzjz

yjghlzjz3#

确保像这样为JpaPropertyMap提供HibernatePersistenceProvider

@Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(dataSource);
        emf.setPackagesToScan("com.example.testservice.entity");
        Map<String, Object> jpaProperties = new HashMap<>();
        jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

        //** Set the JPA provider explicitly (Hibernate)
        emf.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        emf.setJpaPropertyMap(jpaProperties);
        return emf;
    }

相关问题