如何在JBoss EAP 7.2中运行Hibernate OGM?遇持久化提供者缺失报错
Let’s tackle this error step by step—this is a common issue when integrating Hibernate OGM with JBoss EAP, and we can resolve it with targeted checks and configuration adjustments:
1. Verify persistence.xml Location
The persistence.xml file must reside in the src/main/resources/META-INF/ directory of your project. If it’s placed anywhere else (like WEB-INF/ or a custom folder), JBoss EAP won’t scan and load it, leading to the "provider not found" error. Double-check the path and move it if needed.
2. Resolve JBoss EAP Module Conflicts
JBoss EAP 7.2 ships with its own Hibernate Core and JPA modules, which can conflict with the Hibernate OGM dependencies you’ve included. To fix this, create a jboss-deployment-structure.xml file to exclude the server’s built-in Hibernate modules:
Place this file in src/main/webapp/WEB-INF/ (for WAR deployments) or src/main/resources/META-INF/ (for JAR deployments):
<jboss-deployment-structure> <deployment> <exclude-subsystems> <subsystem name="jpa" /> </exclude-subsystems> <exclusions> <module name="org.hibernate" /> <module name="org.hibernate.jpa.api" /> </exclusions> <dependencies> <!-- Include transaction API if you need JTA support --> <module name="javax.transaction.api" export="true" /> </dependencies> </deployment> </jboss-deployment-structure>
This ensures your application uses the Hibernate OGM and Core versions from your pom.xml instead of the server’s built-in ones.
3. Add Missing Hibernate OGM Core Dependency
Your current pom.xml only includes the MongoDB-specific OGM dependency, but Hibernate OGM requires its core module to function. Add this dependency explicitly to your pom.xml:
<dependency> <groupId>org.hibernate.ogm</groupId> <artifactId>hibernate-ogm-core</artifactId> <version>5.4.1.Final</version> </dependency>
While Maven should pull this in as a transitive dependency, explicitly declaring it avoids any issues with dependency resolution or module loading in JBoss.
4. Validate Deployment Package Contents
After building your deployment (WAR/EAR), unzip it and check the WEB-INF/lib folder (for WARs) to confirm these jars are present:
hibernate-ogm-core-5.4.1.Final.jarhibernate-ogm-mongodb-5.4.1.Final.jarhibernate-core-5.3.14.Final.jarhibernate-jpa-2.1-api-1.0.2.Final.jar
If any are missing, check your pom.xml for incorrect <scope> settings (e.g., using provided for dependencies that aren’t supplied by JBoss) or build configuration issues.
5. Adjust Transaction Type (Optional)
If you want JBoss EAP to manage transactions instead of handling them manually, switch the transaction-type in persistence.xml to JTA and add the JTA platform property:
<persistence-unit name="ogm-jpa" transaction-type="JTA"> <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider> <class>com.iexceed.mongopoc.entity.Breed</class> <properties> <!-- Your existing MongoDB properties --> <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/> </properties> </persistence-unit>
This aligns with JBoss’s transaction management capabilities and can avoid potential transaction-related issues down the line.
内容的提问来源于stack exchange,提问作者Saisha

