JPA - No Persistence provider for EntityManager named {{NAME_HERE}}

你。 提交于 2020-01-15 10:20:47

问题


I was about to create a backend for a new app and I got stuck at configuring JPA. I'm using GlassFish 5.0.0 and Hibernate 5.4.5.Final with MySQL.

The error I get is:

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>

and this is 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 is located at project/src/main/resources/META-INF

I access one of my REST endpoints which in turn calls method that is supposed to test JPA, the method basically does this:

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

I can't spot the problem, any suggestions?


回答1:


Did you see this https://stackoverflow.com/a/5121210/4962355 ? It suggests to use hibernate-core.jar instead of the deprecated hibernate-entitymanager.



来源:https://stackoverflow.com/questions/58293564/jpa-no-persistence-provider-for-entitymanager-named-name-here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!