java 无法确定 Hibernate PersistenceProvider

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26192256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:28:36  来源:igfitidea点击:

Failed to determine Hibernate PersistenceProvider

javaspringhibernatejpa

提问by bkowalczyyk

I am trying to configure LocalContainerEntityManagerFactoryBean without persisten.xml file.

我正在尝试在没有 persisten.xml 文件的情况下配置 LocalContainerEntityManagerFactoryBean。

this is my dataSource - it works for Hibernate SessionFactory - so, it is ok.

这是我的数据源 - 它适用于 Hibernate SessionFactory - 所以,没关系。

<bean id="dataSource"
    class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
</bean>

this is my LocalContainerEntityManagerFactoryBean

这是我的 LocalContainerEntityManagerFactoryBean

   <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
   <property name="dataSource" ref="dataSource" />
   <property name="packagesToScan" value="application.models" />
   <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
   </property>
   <property name="jpaProperties">
      <props>
         <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
      </props>
   </property>
</bean>

An exception that i am getting:

我得到的一个例外:

...Could not instantiate bean class [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: Constructor threw exception; nested exception is java.lang.IllegalStateException: Failed to determine Hibernate PersistenceProvider

I read documentation, and i know that LocalContainerEntityManagerFactoryBean has such property, and similar style of creating LocalContainerEntityManagerFactoryBean works in Spring in Action 3 and here: http://softwarecave.org/2014/03/15/using-jpa-and-jta-with-spring/

我阅读了文档,我知道 LocalContainerEntityManagerFactoryBean 具有这样的属性,并且创建 LocalContainerEntityManagerFactoryBean 的类似风格在 Spring in Action 3 和这里工作:http: //softwarecave.org/2014/03/15/using-jpa-and-jta-带弹簧/

Maybe You have an idea what i am doing wrong or at least what spring want tell me via this exception ?

也许你知道我做错了什么,或者至少春天想通过这个例外告诉我什么?

Thanks in advance, Cheers :)

提前致谢,干杯:)

P.S to be clear, Failed to determine Hibernate PersistenceProvider doesn't mean that spring expect persistence.xml - this should be error like: No persistence units parsed from {classpath*:META-INF/persistence.xml}

PS要明确,无法确定Hibernate PersistenceProvider并不意味着spring期望persistence.xml - 这应该是错误,例如:Nopersistence unit parsed from {classpath*:META-INF/persistence.xml}

RESOLVED:

解决:

thanks JB Nizet - if You will have similar problem add:

感谢 JB Nizet - 如果您有类似问题,请添加:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.6.Final</version>
    </dependency>

to pom.xml

到 pom.xml

回答by user3069716

Below code works for me.

下面的代码对我有用。

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource">
    <property  name="jpaVendorAdapter">
         <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
        </bean>
    </property>
</bean>

With hibernate = 4.3.5.Final spring=4.1.4.RELEASE

随着休眠 = 4.3.5.Final spring=4.1.4.RELEASE