JPA提供者vs方言与供应商在Spring连接器配置 [英] JPA provider vs. dialect vs. vendor in the Spring contaniner configuration

查看:396
本文介绍了JPA提供者vs方言与供应商在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>

和persistence.xml jpa文件:

and the persistence.xml jpa file:

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

正如你可以看到jpa提供者相关的信息设置3次。在事务管理器bean中,实体管理器工厂bean和持久化单元配置:

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:

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

但实际上在我的项目中,我只配置了持久化单元with provider。它工作。

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

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

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.

这对我来说不清楚。请解释。

It's no clear to me. Please explain.

推荐答案

将尝试逐行解释:

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

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




  • 此bean定义要使用的jpaDialect。 JpaDialect是一个封装了标准JPA 1.0不提供的某些功能的接口,例如访问底层JDBC连接。此策略主要用于单独使用JPA提供程序;当使用JTA事务运行时,其大部分功能不相关。
    还允许为Spring提供的便携式更强大的EntityManager和EntityManagerFactory子接口提供增值方法。

  • 由于您提供的类为 class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/> ,这允许 Spring 插入供应商特定的行为进入Spring的 EntityManagerFactory 创建者,并且它作为所有供应商特定属性的单一配置点。它是spring自己的 JpaVendorAdapter

    • 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 Spring to plug in vendor-specific behavior into Spring's EntityManagerFactory creators and it serves as single configuration point for all vendor-specific properties.It's a custom implementation of spring's own JpaVendorAdapter.
    • 对于您声明的第二个bean:

      For the second bean where you have declared:

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




      • 你告诉'Spring'配置其属性为 entityManagerFactory jpaDialect 的transactionManager 。因为这些属性必须具体到 hibernate ,这些都是根据设置的。现在将 entityManagerFactory jpaDialect 专门设置为 hibernate 或供应商)。

        • You tell 'Spring' to configure a transactionManager whose properties are entityManagerFactory and jpaDialect. Since these properties have to specific to hibernate these are set according. The entityManagerFactory and jpaDialect are now set specifically to hibernate (or Vendor).
        • 对于第三个bean

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

          < provider> 使用 hibernate 提供程序和类 org.hibernate.ejb.HibernatePersistence 是Hibernate EJB3持久性提供程序实现。

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

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

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

          您的应用程序仅配置持久性和提供程序的原因是供应商适配器自动传递了提供的持久性 HibernatePersistence 通过 JpaVendorAdapter 中的 getPersistenceProvider

          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. HibernatePersistence via the getPersistenceProvider in JpaVendorAdapter.

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

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

          :正如@TheKojuEffect指出的,第一个bean最好是以下形式:

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

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

          谢谢。错过了 vendorAdapter

          您可以参考:

          • HibernateJpaDialect
          • HibernateVendorAdapter
          • HibernatePersistence

          希望它有所帮助。 :)

          Hope it helps. :)

          这篇关于JPA提供者vs方言与供应商在Spring连接器配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆