【问题标题】:How to deal with "No Persistence provider for EntityManager named" error?如何处理“No Persistence provider for EntityManager named”错误?
【发布时间】:2023-02-26 05:33:10
【问题描述】:

尝试在 IntelliJ 中运行 Java Maven 项目时出现错误“No Persistence provider for EntityManager named”,该项目在 Visual Studio Code 中运行良好,没有错误或警告

我不完全确定是什么代码导致了这个问题

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)

这是我的持久性文件

<?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>

已经尝试过:

  • 手动添加 META-INF 到类路径,
  • 将其移动到根文件夹中,
  • 将其移动到资源文件夹中,
  • 将 persistence.xml 移动到根、资源,当然还有 META-INF。
  • 将项目导入 vscode(有效但我希望它在 IntelliJ 中工作)

【问题讨论】:

  • 请分享您用于此设置的 pom.xml 文件。

标签: java hibernate jpa intellij-idea persistence


【解决方案1】:

我在使用 maven archtype 创建的 struts2 项目中遇到了同样的问题我将持久性放在 webapp->WEB-INF 中但它不起作用只是通过转到 main->ressources 解决了这个问题我在里面创建了一个名为 META-INF 的文件夹资源和它工作正常:)enter image description here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多