【发布时间】: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 中的 <provider/>-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