JEE8安全示例迁移至WildFly10:EclipseLink配置失效问题
Let's break down the most likely reasons your persistence.xml isn't working as expected in WildFly 10, especially since you're migrating from GlassFish and using EclipseLink instead of the default Hibernate:
1. Incorrect persistence.xml Location or WildFly's Loading Priority
WildFly follows strict rules for loading JPA configuration files:
- For WAR projects,
persistence.xmlmust reside inWEB-INF/classes/META-INF/(or packaged inside a JAR inWEB-INF/lib/META-INF/for shared persistence units). - If you have JBoss-specific configuration files in
src/main/jboss(likejboss-deployment-structure.xmlorpersistence.xmloverrides), WildFly might prioritize these over your mainpersistence.xml. Double-check that these files aren't overriding your intended settings.
2. Missing or Misconfigured EclipseLink Provider Declaration
Since you're replacing Hibernate with EclipseLink, you need to explicitly specify the provider in persistence.xml—WildFly won't default to it:
<persistence-unit name="YourPU" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <!-- Rest of your config --> </persistence-unit>
Also, ensure your jboss-deployment-structure.xml correctly excludes Hibernate and includes EclipseLink:
<jboss-deployment-structure> <deployment> <!-- Exclude Hibernate to avoid conflicts --> <exclusions> <module name="org.hibernate" /> </exclusions> <!-- Add EclipseLink dependency --> <dependencies> <module name="org.eclipse.persistence" slot="main" /> </dependencies> </deployment> </jboss-deployment-structure>
If you skip this, WildFly will still try to use Hibernate, ignoring your EclipseLink-specific persistence.xml settings.
3. Invalid persistence.xml Schema or Version
Your snippet shows an incomplete xsi:schemaLocation—make sure it's fully specified for JPA 2.1 (which WildFly 10 supports):
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
A malformed schema declaration can cause WildFly to silently fail parsing your persistence.xml.
4. Mismatched DataSource JNDI Name
You mentioned using HSQLDB—verify that the JNDI name in your persistence.xml matches exactly what's configured in WildFly:
<jta-data-source>java:jboss/datasources/YourHSQLDBDataSource</jta-data-source>
WildFly's default HSQLDB datasource is java:jboss/datasources/ExampleDS, but if you created a custom one, ensure the name aligns. A mismatch here will prevent the persistence unit from binding to the datasource, making your config appear ineffective.
5. Cargo Maven Plugin Deployment Issues
Check if Cargo is correctly packaging your persistence.xml into the deployed artifact:
- Run
mvn clean packageand inspect the generated WAR/EAR file—confirmpersistence.xmlis present in the correct location (as noted in point 1). - Ensure Cargo isn't overriding your deployment with pre-configured templates or missing files during the install/deploy phase.
6. Check WildFly Logs for Clues
The fastest way to pinpoint the issue is to look at WildFly's server.log (located in cargo/target/wildfly-10.x.x.Final/standalone/log/):
- Search for keywords like
PersistenceProvider,EclipseLink,persistence-unit, orERRORto find initialization failures, missing dependencies, or parsing errors. - For example, a log entry like
Could not find default persistence providerindicates your provider declaration is missing or incorrect.
内容的提问来源于stack exchange,提问作者robyp7

