为什么需要在 Spring 应用程序上下文中定义 Hibernate-JPA 供应商适配器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9684197/
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-09-08 05:00:37  来源:igfitidea点击:

Why did I need to define a Hibernate-JPA vendor adapter in my Spring application context?

springhibernatedatabase-connection

提问by CFL_Jeff

I spent days and days trying to get a working database connection for my Spring/JPA(Hibernate) integration tests, troubleshooting mysterious "No Database Context Found" errors, and I finally got it working, but I don't understand why I had to do what I did.

我花了几天时间试图为我的 Spring/JPA(Hibernate) 集成测试建立一个有效的数据库连接,解决神秘的“No Database Context Found”错误,我终于让它工作了,但我不明白为什么我必须这样做做我做的。

Note how my LocalContainerEntityManagerFacotryBeanreferences a HibernateJpaVendorAdapter.

请注意我的LocalContainerEntityManagerFacotryBean如何引用HibernateJpaVendorAdapter

applicationContext.xml

应用上下文.xml

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="myEMF">
    <property name="persistenceXmlLocation" value="file:src/test/resources/META-INF/persistence.xml" />
    <property name="persistenceUnitName" value="myPU" />
    <property name="jpaVendorAdapter" ref="hibernateJpaAdapter" />
</bean>

<bean id="hibernateJpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true" />
</bean>

Why did I have to use this HibernateJpaVendorAdapterwhen my persistence unit is already configured for Hibernate as shown below?

为什么我用这个HibernateJpaVendorAdapter时,我的持久性单元已经配置了对Hibernate如下图所示?

persistence.xml

持久化文件

<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
    <class>com.blah.blah.Class1</class>
    <class>com.blah.blah.Class2</class>
    <class>com.blah.blah.Class3</class> 
    <properties>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1?zeroDateTimeBehavior=convertToNull"/>
        <property name="hibernate.connection.username" value="uname"/>
        <property name="hibernate.connection.password" value="pwd"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />

        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.ejb.event.post-insert"
        value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.post-update"
        value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.post-delete"
        value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.pre-collection-update"
        value="org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.pre-collection-remove"
        value="org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.post-collection-recreate"
        value="org.hibernate.envers.event.AuditEventListener" />
        </properties>
</persistence-unit>

It seems to work, but am I doing this right? Could I have done it another/better way?

它似乎有效,但我这样做对吗?我可以用另一种/更好的方式来做吗?

回答by CFL_Jeff

I just found the answer to my question in a post on coderanch.com.

我刚刚在coderanch.com上的一篇文章中找到了我的问题的答案。

Looks like I can circumvent the use of a JpaVendorAdapter if I just specify the JPA provider inside the persistence unit definition in persistence.xml like this:

看起来我可以绕过 JpaVendorAdapter 的使用,如果我只是在 persistence.xml 中的持久性单元定义中指定 JPA 提供者,如下所示:

<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
            .
            .
            .

回答by ManuPK

First of all I am not sure why to use it but I do know that it is not mandatory to use it

首先,我不确定为什么要使用它,但我知道使用它不是强制性的

From the API Docs,

从 API 文档,

Specify the JpaVendorAdapter implementation for the desired JPA provider, if any. This will initialize appropriate defaults for the given provider, such as persistence provider class and JpaDialect, unless locally overridden in this FactoryBean.

为所需的 JPA 提供程序指定 JpaVendorAdapter 实现(如果有)。这将为给定的提供程序初始化适当的默认值,例如持久性提供程序类和 JpaDialect,除非在此 FactoryBean 中本地覆盖。

The way I understand it, is an alternative to the way we specify the hibernate configurations. I did not use it, nor did I find it on Spring API docs. It is not mandatory, let it be initialized to the default value.

我理解它的方式是我们指定休眠配置方式的替代方法。我没有使用它,也没有在 Spring API docs上找到它。它不是强制性的,让它初始化为默认

There are even some issues in spring with jpaVendorAdaptertold in their JIRA. Also there is an SO question here.

甚至jpaVendorAdapter在他们的JIRA 中告诉了spring 中的一些问题。另外还有一个问题,SO这里