【问题标题】:JPA - No Persistence provider for EntityManager named {{NAME_HERE}}JPA - 没有名为 {{NAME_HERE}} 的 EntityManager 的持久性提供程序
【发布时间】:2020-02-06 03:59:44
【问题描述】:

我正要为一个新应用程序创建一个后端,但在配置 JPA 时遇到了困难。 我将GlassFish 5.0.0Hibernate 5.4.5.FinalMySQL 一起使用。

我得到的错误是:

javax.servlet.ServletException: javax.persistence.PersistenceException: No Persistence provider for EntityManager named applicationDB

persistance.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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="applicationDB">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>entities.CityEntity</class>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/world?serverTimezone=GMT"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="PASSWORD_HERE"/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

这是pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>backend</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.4.5.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>


</project>

persistence.xml 位于project/src/main/resources/META-INF

我访问我的 REST 端点之一,该端点又调用应该测试 JPA 的方法,该方法基本上是这样做的:

 EntityManagerFactory emf = Persistence.createEntityManagerFactory("applicationDB");
 EntityManager em = emf.createEntityManager();
 System.out.println("Entity: " + em.find(CityEntity.class, 5));

我找不到问题,有什么建议吗?

【问题讨论】:

    标签: mysql hibernate jpa jakarta-ee glassfish


    【解决方案1】:

    你看到这个https://stackoverflow.com/a/5121210/4962355 了吗? 它建议使用hibernate-core.jar 而不是已弃用的hibernate-entitymanager

    【讨论】:

    • 我将其替换为hibernate-core 版本5.4.6.Final,出现同样的错误。
    • 我猜您确实验证了休眠 JAR 已打包到您的 WAR 存档中?还是您事先将它们部署到 Glassfish?
    • 我正在使用 IntelliJ 并“部署”爆炸的战争档案,我在 IntelliJ 中拥有所有依赖项,包括在 External libraries 下的 Hibernate 核心
    • 好的,那么您可以验证部署的爆炸WAR 是否包含hibernate 类?我不熟悉在 IntelliJ 中部署爆炸档案。
    • 添加了所有 maven 解决的库,仍然一样...,我想我会想办法的,我要感谢您的时间和帮助。
    猜你喜欢
    • 2014-12-07
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 2011-03-14
    • 1970-01-01
    • 2015-11-08
    相关资源
    最近更新 更多