1

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?

whatamidoingwithmylife
  • 1,119
  • 1
  • 16
  • 35

1 Answers1

1

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

Christoph John
  • 3,003
  • 2
  • 13
  • 23
  • I replaced it with `hibernate-core` version `5.4.6.Final`, same error occurs. – whatamidoingwithmylife Oct 08 '19 at 20:47
  • I guess you did verify that the hibernate JARs are packaged into your WAR archive? Or did you deploy them to Glassfish beforehand? – Christoph John Oct 08 '19 at 21:07
  • I'm using IntelliJ and "deploying" exploded war archive, I have all the dependencies including Hibernate core under `External libraries` in IntelliJ – whatamidoingwithmylife Oct 08 '19 at 21:26
  • OK, so you can verify that the deployed exploded WAR contains the hibernate classes? I am not familiar with deploying exploded archives in IntelliJ. – Christoph John Oct 08 '19 at 21:34
  • I'm unsure, when I check the generated artifact, it contains no dependencies (META-INF/lib doesn't exist), but my assumption is that when it's exploded deployment that IntelliJ is supposed to link to necessary dependencies? – whatamidoingwithmylife Oct 08 '19 at 21:43
  • Hmm, not sure on that part. I mean somehow Glassfish needs to have the libraries available. I don't know how IntelliJ achieves this. You could try copying the hibernate and mysql libs to the `lib/ext` directory of your Glassfish domain and see if that helps. Don't forget to restart GF after that. – Christoph John Oct 08 '19 at 21:46
  • OK, so I did `asadmin add-library --type ext PATH_TO_JAR` for Hibernate core and then after I ran the server again I got `org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: javax/persistence/spi/PersistenceProvider` then I did the same for `javax.persistence-api-2.2.jar` and now I'm back at: `javax.persistence.PersistenceException: No Persistence provider for EntityManager named applicationDB` – whatamidoingwithmylife Oct 08 '19 at 22:03
  • Hmm, strange. If there isn't any underlying Exception (I think Hibernate/GF also needs the mysql libraries, did you deploy them?) I am out of ideas. Sorry. – Christoph John Oct 08 '19 at 22:16
  • 1
    Added all of the libs maven resolved, still same..., well I'll figure something out I guess, I want to thank you for your time and help efforts. – whatamidoingwithmylife Oct 08 '19 at 22:31