Java8 Spring Boot应用部署IBM WebSphere 8.5.5.21启动失败求助
本地运行正常,但部署到IBM WebSphere 8.5.5.21后启动失败,报错显示javax.persistence.Table.indexes()方法不存在,提示Hibernate的EntityBinder与JPA的Table类版本不兼容。
错误日志
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. [3/2/23 18:10:22:903 IST] 0000028e SystemOut O 2023-03-02 18:10:22.902 ERROR 1023660 --- [ebContainer : 4] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:1239) The following method did not exist: javax/persistence/Table.indexes()[Ljavax/persistence/Index; The calling method's class, org.hibernate.cfg.annotations.EntityBinder, was loaded from the following location: wsjar:file:/opt/IBM/WebSphere/AppServer/profiles/AppSrv03/installedApps/BSIBWASNode03Cell/Schedulers_war.ear/Schedulers.war/WEB-INF/lib/hibernate-core-5.6.14.Final.jar!/org/hibernate/cfg/annotations/EntityBinder.class The called method's class, javax.persistence.Table, is available from the following locations: bundleresource://234.fwk804200143/javax/persistence/Table.class jar:file:/opt/IBM/WebSphere/AppServer/lib/j2ee.jar!/javax/persistence/Table.class wsjar:file:/opt/IBM/WebSphere/AppServer/profiles/AppSrv03/installedApps/BSIBWASNode03Cell/Schedulers_war.ear/Schedulers.war/WEB-INF/lib/jakarta.persistence-api-2.2.3.jar!/javax/persistence/Table.class The called method's class hierarchy was loaded from the following locations: javax.persistence.Table: file:/opt/IBM/WebSphere/AppServer/plugins/javax.j2ee.persistence.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes org.hibernate.cfg.annotations.EntityBinder and javax.persistence.Table
pom.xml 配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.7</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.kgisl.bsibschedulers</groupId> <artifactId>BSIB-Schedulers</artifactId> <version>1.0.0</version> <packaging>war</packaging> <name>bsibschedulers</name> <description>BSIB Scheduler services</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-legacy</artifactId> <version>1.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <finalName>Schedulers</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
主类代码
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class BsibschedulersApplication extends SpringBootServletInitializer { public static void main(String[] args) { System.out.println("BsibschedulersApplication Started"); SpringApplication.run(BsibschedulersApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { System.out.println("BsibschedulersApplication Configure"); return application.sources(BsibschedulersApplication.class); } }
问题根源
WebSphere 8.5.5.21自带JPA 2.0版本,而Spring Boot 2.7.7依赖的Hibernate 5.6.x需要JPA 2.2支持,javax.persistence.Table.indexes()是JPA 2.1新增的方法,WebSphere自带的旧版本JPA没有该方法。同时WebSphere默认优先加载自身库中的类,导致应用的Hibernate与WebSphere的JPA版本冲突。
解决步骤
排除Spring Boot JPA依赖中的JPA API
修改spring-boot-starter-data-jpa依赖,排除jakarta.persistence-api,避免应用打包自带JPA API:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <exclusions> <exclusion> <groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> </exclusion> </exclusions> </dependency>调整WebSphere类加载策略
在WebSphere控制台修改应用配置:- 进入应用管理页面,找到目标应用,点击类加载和更新检测
- 将类加载顺序设为
类加载器的类路径优先(让应用自身类优先于WebSphere系统库加载) - 将WAR类加载器策略设为
每个WAR文件使用单独的类加载器
降级Hibernate版本(可选)
若类加载策略调整后仍有问题,可降级Hibernate到兼容JPA 2.0的版本(如Hibernate 4.3.x),在pom.xml中指定版本:<properties> <hibernate.version>4.3.11.Final</hibernate.version> </properties>注意:降级需确认Spring Data JPA与Hibernate版本兼容性,Spring Boot 2.7.x默认适配Hibernate 5.6,降级后可能需调整其他依赖。
移除冗余依赖
删除pom.xml中的javax.servlet-api依赖,WebSphere已自带兼容版本,避免冲突。
内容的提问来源于stack exchange,提问作者Sakthi Prasad S

