我最近继承了一个 Spring/Hibernate 应用程序,并将其升级为Spring 5.2.8
, SpringSecurity 5.3.4
, Hibernate 5.4.21
.
我们正在部署Websphere 8.5.5
(完全,而不是自由)。
当我尝试运行该应用程序时,我得到一个异常,我认为相关部分是:
...嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/applicationContext-hibernate.xml] 中定义名称为“auditSessionFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 java.lang.NoSuchMethodError: javax/persistence/JoinColumn.foreignKey()Ljavax/persistence/ForeignKey; (从文件:/C:/Program Files (x86)/IBM/WebSphere/AppServer/plugins/javax.j2ee.persistence.jar 由 org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader@2472a612 加载)从类 org 调用。 hibernate.cfg.AnnotationBinder(从文件加载:/C:/Program%20Files%20(x86)/IBM/WebSphere/AppServer/profiles/AppSrv01/installedApps/Node01Cell/nameof_war.ear/nameof.war/WEB-INF/lib /hibernate-core-5.4.21.Final.jar 来自 com.ibm.ws.classloader.CompoundClassLoader@71ed602b[war:nameof_war/nameof.
我注意到它使用的是 JPA 的 Websphere 版本,而不是我在战争中包含在 jar 中的那个。Websphere 附带javax.j2ee.persistence.jar
- JPA 版本2.0
,它解释了错误。
对另一个 SO 问题的回答让我来到这里:https ://www.ibm.com/support/knowledgecenter/en/SSAW57_8.5.5/com.ibm.websphere.nd.multiplatform.doc/ae/tejb_jpa3rdparty.html
此链接试图解释如何使用第三方持久性提供程序。
问题是,链接说要使用persistence.xml
;但我继承的应用程序没有那个 xml 文件。它只有applicationContext-hibernate.xml
和 orm.xml。
我试过创建一个persistence.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"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="nameof">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jar-file>nameof.jar</jar-file>
</persistence-unit>
</persistence>
出现在 WAR 文件中的正确位置;但我不知道这是否正确 - 也不知道我是否还必须更新任何其他文件?我是否需要在其他地方引用该持久性单元?
我还将 WebSphere 中的类加载器更新为最后一个父类;但我仍然收到如上所示的错误。
我错过了哪些步骤?我需要在applicationContext-hibernate.xml
(或其他地方)做些什么改变才能让它发挥作用?
我的 applicationContext-hibernate.xml
样子:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="auditSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.name.class1</value>
<value>com.name.class2</value>
<value>etc</value>
</list>
</property>
<property name="mappingResources">
<list>
<value>com/name/model/orm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle12cDialect</prop>
<prop key="hibernate.jdbc.fetch_size">100</prop>
</props>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.name.class3</value>
<value>com.name.class4</value>
<value>etc</value>
</list>
</property>
<property name="mappingResources">
<list>
<value>com/name/model/orm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle12cDialect</prop>
<prop key="hibernate.jdbc.fetch_size">100</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.jdbc.batch_size">100</prop>
</props>
</property>
<property name="entityInterceptor">
<bean class="com.name.interceptor.HibernateInterceptor">
<property name="auditInterceptor">
<bean class="com.name.interceptor.AuditInterceptor" />
</property>
<property name="comparativeAuditInterceptor">
<bean class="com.name.interceptor.ComparativeAuditInterceptor">
<property name="cadDao">
<bean class="com.name.dao.ComparativeAuditDetailsDaoImpl">
<property name="sessionFactory" ref="auditSessionFactory" />
</bean>
</property>
<property name="undoDao">
<bean class="com.name.dao.UndoDaoImpl">
<property name="sessionFactory" ref="auditSessionFactory" />
</bean>
</property>
</bean>
</property>
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate5.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionInterceptor" class="com.name.TransactionalWebRequestInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttribute" value="PROPAGATION_REQUIRES_NEW"/>
</bean>
</beans>