【问题标题】:How to use WAS standard persistence provider with Spring如何在 Spring 中使用 WAS 标准持久性提供程序
【发布时间】:2014-07-07 08:10:55
【问题描述】:

我正在开发一个在 WebSphere Application Server 中运行的 portlet(如果它是一个 servlet 而不是一个 portlet,我接受同样的问题)。目前它依赖于休眠。由于 WAS 本身提供了 JPA 实现,它是 OpenJPA 2.0 的修改版本,我想摆脱 Hibernate。

这是我的设置。持久性.xml:

  <?xml version="1.0" encoding="UTF-8"?>
  <persistence xmlns="http://java.sun.com/xml/ns/persistence"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  >
      <persistence-unit name="default" transaction-type="JTA">
          <provider>org.hibernate.ejb.HibernatePersistence</provider>

          <jta-data-source>jdbc/myDb</jta-data-source>
          <properties>
              <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform" />
              <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />
          </properties>
      </persistence-unit>
  </persistence>

myPortlet-portlet.xml

  <!-- ... -->

  <tx:jta-transaction-manager />
  <jee:jndi-lookup jndi-name="jdbc/myDb" cache="true" id="dataSource" expected-type="javax.sql.DataSource" />

  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="default" />
  </bean>

在我的 DAO 类中,我使用注释访问 entityManager:

  @PersistenceContext(unitName = "default")
  private EntityManager entityManager;

使用 Hibernate 一切正常。

根据 WebSphere Application Server 文档,如果您未通过使用 persistence.xml 中的 &lt;provider/&gt;-tag 指定默认持久性提供程序,则使用默认持久性提供程序。但是在注释掉提供者规范后,Spring由于找不到提供者类而抛出异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in PortletContext resource [/WEB-INF/myPortlet-portlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No PersistenceProvider specified in EntityManagerFactory configuration, and chosen PersistenceUnitInfo does not specify a provider class name either

如何将提供的 JPA 实现与 Spring (Portlet) MVC 一起使用?

【问题讨论】:

  • 什么是WAS版本? (以防万一,例如,您可能需要版本 7 的功能包)。我们实际上在persistence.xml中使用了org.apache.openjpa.persistence.PersistenceProviderImpl
  • 版本 8.0。如果我使用 org.apache.openjpa.persistence.PersistenceProviderImpl,我实际上使用的是 OpenJPA(也是 WAS 提供的)。但我想使用他们修改后的 OpenJPA 版本
  • 我猜你是对的。但是省略提供者可能只适用于 EJB 配置。可能 spring 不能正确处理它。

标签: spring-mvc jpa websphere jpa-2.0 spring-portlet-mvc


【解决方案1】:

这很可能发生,因为您在应用程序中包含的 Spring JAR 包含 Persistence 类或其他 JPA 类的不同实现,用于“引导”JPA。

如果您想使用 WebSphere 的默认提供程序(或者更准确地说,使用通过 WebSphere 的管理屏幕配置的任何 JPA 提供程序),那么您必须确保在运行时调用的 JPA“引导”代码是 WebSphere 的,而不是你的。

您应该寻找一个不会与 JPA 混淆的 Spring 分发 JAR。

【讨论】:

  • 如果我找到这样的 Spring 发行版,我只能验证您的答案。你认识一个吗?
【解决方案2】:

简答

如果要使用 LocalContainerEntityManagerFactoryBean,则不能通过省略提供者来使用 WebSphere 的默认提供者。

长答案

通常,实体管理器由容器提供的实体管理器工厂创建。您可以通过手动执行上下文 loopkup (EntityManager em = (EntityManager) ctx.lookup(...)) 或使用 Springs jndi-lookup 功能来检索它:

<beans>
    <jee:jndi-lookup id="myEmf" jndi-name="persistence/myPersistenceUnit"/>
</beans>

在问题中,使用了一种不同的方法,LocalContainerEntityManagerFactoryBean,它自己创建了一个实体管理器工厂。这个实体管理器工厂是一个代理,它实现了本地实体管理器工厂的所有接口。为了创建这样的代理,Spring 必须知道本地实体管理器工厂的类。 Spring 使用三种不同的方式来确定类:

  1. 通过 persistence.xml 中的&lt;provider/&gt;-entry 检测它
  2. 询问 jpaVendorAdapter(在工厂 bean 的同名属性中指定)
  3. 使用工厂 bean 的 entityManagerFactoryInterface-property

这就是为什么你不能完全忽略你的提供者的规范。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 2018-04-03
    • 2014-01-25
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多