如何在Wildfly 11中将Hibernate升级至5.2.x适配原生Hibernate使用场景?
Alright, let's break down how to upgrade your Hibernate version to 5.2.x in your Spring + WAR app running on WildFly 11—since you're using native Hibernate, there are a few key steps to avoid classloading conflicts with WildFly's built-in Hibernate 5.1.10.Final. Here's what you need to do:
1. Exclude WildFly's Built-in Hibernate Modules
First, you need to tell WildFly not to use its own Hibernate version, because we'll be bundling our own 5.2.x copy. Update your jboss-deployment-structure.xml to exclude the core Hibernate module (and any related sub-modules that might cause conflicts):
<jboss-deployment-structure> <deployment> <exclusions> <!-- Exclude WildFly's default Hibernate core --> <module name="org.hibernate" /> <!-- Exclude related modules if you run into conflicts --> <module name="org.hibernate.commons-annotations" /> <!-- Optional: Exclude WildFly's JPA API if you're bundling a newer version --> <module name="javax.persistence" /> </exclusions> <!-- Keep your other necessary dependencies here (e.g., Spring-related modules) --> <dependencies> <!-- Example: <module name="org.springframework" /> (if using WildFly's Spring module) --> </dependencies> </deployment> </jboss-deployment-structure>
2. Add Hibernate 5.2.x Dependencies to Your Project
Next, include the Hibernate 5.2.x libraries in your project (make sure they're bundled into your WAR's WEB-INF/lib directory). If you're using Maven, add these dependencies to your pom.xml:
<!-- Hibernate Core 5.2.x (note: hibernate-entitymanager is merged into core in 5.2+) --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.18.Final</version> <!-- Pick a stable 5.2.x release --> <scope>compile</scope> </dependency> <!-- JPA API (match the version supported by Hibernate 5.2, which is JPA 2.2) --> <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> <scope>compile</scope> </dependency>
Note: Hibernate 5.2 merged the
hibernate-entitymanagermodule intohibernate-core, so you don't need to include it separately anymore.
3. Check Compatibility & Adjust Code
Hibernate 5.2 introduced some API changes and deprecations, plus you need to ensure Spring plays nicely with it:
- Spring Version: Make sure you're using Spring 4.3.x or later—older Spring versions don't support Hibernate 5.2. If your Spring version is outdated, upgrade it first.
- API Changes: Replace any deprecated Hibernate code. For example, the old
Configuration.buildSessionFactory()method is deprecated; useStandardServiceRegistryBuilderinstead to create yourSessionFactory. - Dependency Conflicts: Watch for conflicting libraries (e.g., older versions of Hibernate commons annotations) in your
WEB-INF/lib—use Maven'sdependency:treecommand to identify and resolve these.
4. Test Deployment & Troubleshoot
Package your WAR and deploy it to WildFly 11. Check the server logs closely for:
NoClassDefFoundErrororClassNotFoundException: These usually mean you're missing a dependency or forgot to exclude a WildFly module.ClassCastException: This indicates a classloading conflict (e.g., two versions of the same class loaded from different sources). Double-check your exclusions injboss-deployment-structure.xml.
If you run into issues, verify that all WildFly-provided Hibernate-related modules are excluded, and that your bundled dependencies are the correct versions.
内容的提问来源于stack exchange,提问作者dominik

