我目前正在将旧版 Websphere 应用程序/服务移至 tomcat 上运行。 除了需要访问 sqlServer 数据库的特定调用之外,这对于服务需要执行的大部分操作都可以正常工作。
尝试加载上下文时出现以下错误:
ClassPathXmlApplicationContext : Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [SpringConfigDataAccess.xml]:
Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
但是和使用jpa 2.1好像有冲突 我怀疑它可能是为了使应用程序在 tomcat 上运行而添加的 WAS 库之一或数据库配置中的 WAS 特定类。
这里有一些一般信息和依赖关系树:
WAS 使用 ibmjdk for java 1.8
Tomcat 使用 openJDK for java 1.8
之前的配置
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/dbName"/>
<property name="lookupOnStartup" value="false"/>
<property name="cache" value="true" />
<property name="proxyInterface" value="javax.sql.DataSource" />
</bean>
新配置
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp2.BasicDataSource" scope="prototype">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="username" value="username" />
<property name="password" value="pass" />
<property name="url" value="server_url"/>
</bean>
这个改变需要在maven项目依赖中添加commons-dbcp2
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.10.0</version>
</dependency>
该数据库的会话工厂配置是:
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
...
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.max_fetch_depth">6</prop>
<prop key="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</prop>
<!-- IBM WAS SPECIFIC -->
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</prop>
<prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WebSphereJTATransactionLookup</prop>
<prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WebSphereJtaPlatform</prop>
<!-- /END IBM WAS SPECIFIC -->
<!-- added in order for a JUnit test to load properly -->
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
<!-- Previous WAS SPECIFIC transaction manager -->
<!-- <bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/>-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"/>
最后是依赖树
+- com.project.app:main-project-compose:jar:1.0.0-SNAPSHOT:compile
| +- com.project-common.data-access:data-access:jar:5.0.0.2:compile
| | +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
| | \- com.microsoft:sqlserver.jdbc:jar:4.0:runtime
| +- org.hibernate:hibernate-core:jar:5.6.4.Final:compile
| | +- org.jboss.logging:jboss-logging:jar:3.4.3.Final:compile
| | +- net.bytebuddy:byte-buddy:jar:1.12.7:compile
| | +- antlr:antlr:jar:2.7.7:compile
| | +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.1.1.Final:compile
| | +- org.jboss:jandex:jar:2.4.2.Final:compile
| | +- com.fasterxml:classmate:jar:1.5.1:compile
| | +- javax.activation:javax.activation-api:jar:1.2.0:compile
| | +- org.hibernate.common:hibernate-commons-annotations:jar:5.1.2.Final:compile
| | +- javax.xml.bind:jaxb-api:jar:2.3.1:compile
| | \- org.glassfish.jaxb:jaxb-runtime:jar:2.3.1:compile
| | +- org.glassfish.jaxb:txw2:jar:2.3.1:compile
| | +- com.sun.istack:istack-commons-runtime:jar:3.0.7:compile
| | +- org.jvnet.staxex:stax-ex:jar:1.8:compile
| | \- com.sun.xml.fastinfoset:FastInfoset:jar:1.2.15:compile
| \- org.apache.commons:commons-collections4:jar:4.1:compile
+- org.springframework:spring-context:jar:5.3.13:compile
| +- org.springframework:spring-aop:jar:5.3.13:compile
| +- org.springframework:spring-beans:jar:5.3.13:compile
| +- org.springframework:spring-core:jar:5.3.13:compile
| | \- org.springframework:spring-jcl:jar:5.3.13:compile
| \- org.springframework:spring-expression:jar:5.3.13:compile
+- org.springframework:spring-orm:jar:5.3.13:compile
| +- org.springframework:spring-jdbc:jar:5.3.13:compile
| \- org.springframework:spring-tx:jar:5.3.13:compile
+- junit:junit:jar:4.11:test
| \- org.hamcrest:hamcrest-core:jar:1.3:test
+- xerces:xercesImpl:jar:2.12.2:compile
| \- xml-apis:xml-apis:jar:1.4.01:compile
+- com.commons:websphere-mq-dependencies:pom:7.0.1.5.0:test
| +- com.ibm.websphere:com.ibm.mq:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.mqjms:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.mq.jmqi:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.mq.jmqi.local:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.mq.jmqi.remote:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.mq.jmqi.system:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.msg.client.commonservices:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.msg.client.jms.internal:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.msg.client.jms:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.msg.client.provider:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.msg.client.wmq:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.msg.client.wmq.common:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.mq.connector:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.mq.commonservices:jar:7.0.1.5:test
| +- com.ibm.websphere:com.ibm.mq.headers:jar:7.0.1.5:test
| \- com.ibm.websphere:com.ibm.mq.pcf:jar:7.0.1.5:test
+- com.ibm.websphere:com.ibm.ws.ejb.thinclient:jar:8.0.0:test
+- com.project.app:main-project-packaging:jar:1.0.0-SNAPSHOT:compile
| +- org.apache.pdfbox:pdfbox:jar:3.0.0-alpha3:compile
| | +- org.apache.pdfbox:io:jar:3.0.0-alpha3:compile
| | \- org.apache.pdfbox:fontbox:jar:3.0.0-alpha3:compile
| +- org.bouncycastle:bcprov-jdk18on:jar:1.72:compile
| +- net.sf.opencsv:opencsv:jar:2.4:compile
| \- com.commons:dependencies-emf-ecore-sdo:pom:2.2.1.0:runtime
| +- org.eclipse.persistence:commonj.sdo:jar:2.1.1:runtime
| +- org.eclipse.emf:org.eclipse.emf.ecore:jar:2.2.1.v200609210005:runtime
| +- org.eclipse.emf:org.eclipse.emf.common:jar:2.2.1.v200609210005:runtime
| +- org.eclipse.emf:org.eclipse.emf.ecore.sdo:jar:2.2.0.v200609210005:runtime
| +- org.eclipse.emf:org.eclipse.emf.ecore.xmi:jar:2.2.1.v200609210005:runtime
| \- org.eclipse.emf:org.eclipse.emf.ecore.change:jar:2.2.0.v200609210005:runtime
+- javax.persistence:javax.persistence-api:jar:2.2:compile
\- org.apache.commons:commons-dbcp2:jar:2.10.0:compile
+- org.apache.commons:commons-pool2:jar:2.11.1:compile
+- commons-logging:commons-logging:jar:1.2:compile
\- jakarta.transaction:jakarta.transaction-api:jar:1.3.1:compile
这通常是与 JPA(Java Persistence API)库的版本冲突。 NoSuchMethodError 表明应用程序正在尝试调用 javax.persistence.Table 类上的方法indexes(),该方法在JPA 正在使用的版本中不存在。
根据我基于您的依赖关系树的(简短)分析,您正在使用
所以你必须决定要使用哪一个,哪个是包含方法“javax.persistence.Table.indexes()”
javax.persistence:javax.persistence-api:jar:2.2 看起来应该包含 javax.persistence.Table.indexes() 方法(您应该检查一下!)。
现在,我们的想法是尝试从其他依赖项中排除所有其他 JPA,如下所示:
<dependency>
<groupId>some.group</groupId>
<artifactId>conflicting-artifact</artifactId>
<version>conflicting-version</version>
<exclusions>
<exclusion>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</exclusion>
</exclusions>
</dependency>
由于您将 Hibernate 作为 JPA 提供程序,因此您可以使用 Hibernate 的内部 JPA 实现,该实现包含在 Hibernate Core 中。
清理然后重建。如果问题仍然存在,请尝试打印应用程序在启动期间使用的类路径,以帮助识别正在加载的任何意外 JPA 库。
祝你好运:)