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

IntelliJ运行Maven项目时遇“No Persistence provider...”错误排查

IntelliJ中Maven JPA项目报错:No Persistence provider for EntityManager named default

问题现象

运行项目时抛出以下异常:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named default
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at JNWR.ServerApplication.<clinit>(ServerApplication.java:18)

该项目在Visual Studio Code中可正常运行,无任何报错或警告。

已尝试的操作

  • 手动将META-INF添加至类路径
  • 将persistence.xml移至根目录、resources目录及META-INF目录下

提供的persistence.xml配置

<?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-unit name="default" transaction-type="RESOURCE_LOCAL">
        <description>
            Persistence unit for the JNWR Database
        </description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>JNWR.Entity.Customer</class>
        <properties>
            <property name="javax.persistence.jdbc.driver"
                      value="com.mysql.cj.jdbc.Driver" />

            <property name="javax.persistence.jdbc.url"
                      value="jdbc:mysql://localhost:3306/jwrms_db" />

            <property name="javax.persistence.jdbc.user"
                      value="defaultUser"/>

            <property name="javax.persistence.jdbc.password"
                      value="defaultPassword%"/>

            <property name="hibernate.dialect" 
                        value="org.hibernate.dialect.MySQL8Dialect" />
                        
            <property name="hibernate.show_sql"
                      value="true" />

        </properties>

    </persistence-unit>

</persistence>

解决方案

针对IntelliJ的特性,按以下步骤排查修复:

1. 规范资源目录结构

确保persistence.xml放在src/main/resources/META-INF目录下:

  • 若该目录不存在,手动右键项目 -> New -> Directory,依次创建src/main/resources/META-INF
  • 将persistence.xml移入该目录

2. 刷新Maven依赖并重新构建

  • 点击右侧Maven面板的刷新按钮(🔄),确保Hibernate等JPA依赖正确下载
  • 执行Maven生命周期命令:双击clean再双击compile,或直接点击工具栏Build -> Rebuild Project

3. 检查运行配置的类路径

  • 打开右上角运行按钮旁的下拉菜单 -> Edit Configurations
  • 切换到Classpath标签页,确认当前项目模块已包含在依赖中,且resources目录标记为蓝色资源根目录
    • 若未标记,右键resources目录 -> Mark Directory as -> Resources Root

4. 验证Hibernate依赖完整性

检查pom.xml是否包含兼容JPA 2.2的Hibernate核心依赖,示例如下:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.6.14.Final</version>
</dependency>
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>

缺失则添加后重新刷新Maven。

5. 清除IntelliJ缓存并重启

  • 点击File -> Invalidate Caches...
  • 勾选"Clear file system cache and local history",点击Invalidate and Restart
  • 重启后重新构建项目

内容的提问来源于stack exchange,提问作者G.T Grace

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 12:10:38