我正在开发一个在 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 一起使用?