You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Glassfish服务器JPA连接MySQL报错:找不到em1的持久化提供者

Fixing "No Persistence provider for EntityManager named em1" in GlassFish + JPA

Hey there! Let's work through this super common JPA error you're hitting. The No Persistence provider message almost always points to issues with your persistence.xml—either it's in the wrong place, has syntax errors, or missing critical config. Here are the key fixes to try:

1. Double-check the persistence.xml location

GlassFish expects this file to live in a very specific spot:

  • For Maven/Gradle projects: src/main/resources/META-INF/persistence.xml
  • For traditional web projects: WEB-INF/classes/META-INF/persistence.xml

If it's anywhere else (like directly in src/main/resources or WEB-INF), GlassFish won't find it. After moving it, rebuild/deploy your project to make sure the file gets copied to the right location in the deployed app.

2. Verify the persistence unit name matches exactly

Your code is calling Persistence.createEntityManagerFactory("em1"), so your persistence.xml must have a persistence unit with exactly that name—no typos, no case differences. It should look like this:

<persistence-unit name="em1" transaction-type="JTA">
  <!-- rest of config -->
</persistence-unit>

3. Explicitly specify the persistence provider

GlassFish uses EclipseLink by default, but sometimes auto-detection fails. Add this line inside your <persistence-unit> block to force it:

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

If you're using Hibernate instead, replace the provider with org.hibernate.jpa.HibernatePersistenceProvider—just make sure the Hibernate JPA jar is in GlassFish's domain/lib folder or your project's WEB-INF/lib.

4. Fix database connection config

Since you're connecting to a remote MySQL database, confirm these details in the <properties> section of your persistence unit:

<properties>
  <!-- Use the modern MySQL driver -->
  <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
  <!-- Replace with your remote host, port, and DB name -->
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://your-remote-ip:3306/your-db?useSSL=false&serverTimezone=UTC"/>
  <property name="javax.persistence.jdbc.user" value="your-db-username"/>
  <property name="javax.persistence.jdbc.password" value="your-db-password"/>
  <!-- Optional: Enable logging to debug connection issues -->
  <property name="eclipselink.logging.level" value="FINE"/>
</properties>

Also, make sure the MySQL connector jar (mysql-connector-java-x.x.x.jar) is in GlassFish's domain/lib directory—this is required for remote DB access.

5. Validate the XML syntax

Even tiny syntax errors (unclosed tags, mismatched quotes, wrong namespace) can break the file. Use your IDE's XML validation tool (most IDEs like IntelliJ or Eclipse will highlight errors) to check for issues. Your root <persistence> tag should have the correct namespace and schema, like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_2.xsd"
             version="2.2">
  <!-- persistence units here -->
</persistence>

6. Confirm the file is deployed correctly

After deploying to GlassFish, navigate to your app's deployed folder (usually domain1/applications/your-app-name/WEB-INF/classes/META-INF) and check if persistence.xml exists there. If not, your build process isn't copying it over—for Maven, run mvn clean package and check the target/classes/META-INF folder to verify the file is included.

If you still run into issues, share your full persistence.xml content and we can dig deeper!

内容的提问来源于stack exchange,提问作者quantumbutterfly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:28:56