Java Spring 容器配置中的 JPA 提供者与方言与供应商

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

JPA provider vs. dialect vs. vendor in the Spring contaniner configuration

javaspringhibernatejpaconfiguration

提问by Alexandr

Example of spring configuration file:

spring配置文件示例:

<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory"ref="entityManagerFactory"/>
    <property name="jpaDialect"ref="jpaDialect"/>
</bean>

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
....
</bean>

and the persistence.xml jpa file:

和persistence.xml jpa 文件:

<persistence-unit name="EmployeeService">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>

As you can see the jpa provider-related information is set 3 times. In transaction manager bean, entity manager factory bean and in the persistence unit configuration:

如您所见,jpa 提供程序相关信息设置了 3 次。在事务管理器 bean、实体管理器工厂 bean 和持久化单元配置中:

<property name="jpaDialect"ref="jpaDialect"/>
...
<property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
...
<provider>org.hibernate.ejb.HibernatePersistence</provider>

But actually in my project I configured only the persistence unit with provider. And it worked.

但实际上在我的项目中,我只配置了带有提供程序的持久性单元。它奏效了。

So my question is what's the difference between provider, dialect and vendor options? Must I set all of them or, I can skip some of them? Can I set, for example as a vendor for EntityMangerFactory - Hibernate, as a dialect in transaction manager - Eclipse and as a provider in the persistence unit configuration - something else, TopLink, for example.

所以我的问题是提供者、方言和供应商选项之间有什么区别?我必须设置所有这些还是可以跳过其中一些?例如,我可以设置为 EntityMangerFactory - Hibernate 的供应商、事务管理器 - Eclipse 中的方言以及持久性单元配置中的提供者 - 其他东西,例如 TopLink。

It's no clear to me. Please explain.

我不清楚。请解释。

采纳答案by user2339071

Will try to explain it to you line by line:

将尝试逐行向您解释:

<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>

//Should ideally be 
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
  • This bean defines the jpaDialect that you are going to use. JpaDialect is an interface encapsulates certain functionality that standard JPA 1.0 does not offer, such as access to the underlying JDBC Connection. This strategy is mainly intended for standalone usage of a JPA provider; most of its functionality is not relevant when running with JTA transactions. Also allows for the provision of value-added methods for portable yet more capable EntityManager and EntityManagerFactory subinterfaces offered by Spring.
  • Since you have provided the class as class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>, this allows Springto plug in vendor-specific behavior into Spring's EntityManagerFactorycreators and it serves as single configuration point for all vendor-specific properties.It's a custom implementation of spring's own JpaVendorAdapter.
  • 这个 bean 定义了您将要使用的 jpaDialect。JpaDialect 是一个接口,封装了标准 JPA 1.0 不提供的某些功能,例如对底层 JDBC 连接的访问​​。此策略主要用于 JPA 提供程序的独立使用;当使用 JTA 事务运行时,它的大部分功能都无关紧要。还允许为 Spring 提供的可移植但功能更强大的 EntityManager 和 EntityManagerFactory 子接口提供增值方法。
  • 由于您已将类提供为class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>,这允许Spring将特定于供应商的行为插入到 Spring 的EntityManagerFactory创建者中,并且它充当所有特定于供应商的属性的单个配置点。它是Spring自己的JpaVendorAdapter.

For the second bean where you have declared:

对于您声明的第二个 bean:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory"ref="entityManagerFactory"/>
    <property name="jpaDialect"ref="jpaDialect"/>
</bean>
  • You tell 'Spring' to configure a transactionManagerwhose properties are entityManagerFactoryand jpaDialect. Since these properties have to specific to hibernatethese are set according. The entityManagerFactoryand jpaDialectare now set specifically to hibernate(or Vendor).
  • 您告诉 'Spring' 配置一个transactionManager属性为entityManagerFactory和 的对象jpaDialect。由于这些属性要具体到hibernate这些来设置。的entityManagerFactoryjpaDialect现在被特别设置为hibernate(或供应商)。

As for the third bean

至于第三豆

<property name="jpaDialect"ref="jpaDialect"/>
...
<property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
...
<provider>org.hibernate.ejb.HibernatePersistence</provider>

The <provider>tells spring to use the hibernateprovider and the class org.hibernate.ejb.HibernatePersistenceis Hibernate EJB3 persistence provider implementation.

<provider>告诉Spring使用hibernate提供者和类org.hibernate.ejb.HibernatePersistence是Hibernate的EJB3持久性提供者实现。

In short, you need to configure these in order to tell spring which ORM's functionality should be used.

简而言之,您需要配置这些以告诉 spring 应该使用哪些 ORM 功能。

The reason that your application worked with configuring just persistence and provider is because the vendor adapter is automatically passed the persistence provided i.e. HibernatePersistencevia the getPersistenceProviderin JpaVendorAdapter.

您的应用程序仅配置持久性和提供程序的原因是因为供应商适配器会自动传递提供的持久性,即HibernatePersistence通过getPersistenceProviderin JpaVendorAdapter

Tinker around the documentation to understand how these classes are interlinked.

修改文档以了解这些类是如何相互关联的。

EDIT: As pointed out by @TheKojuEffect , the first bean should ideally be in the form of :

编辑:正如@TheKojuEffect 所指出的,理想情况下,第一个 bean 应该采用以下形式:

<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>

Thanks. Missed the vendorAdapter.

谢谢。错过了vendorAdapter

You can refer :

你可以参考:

Hope it helps. :)

希望能帮助到你。:)