1

I have the following project structure which is a multi-module maven project. My issue is in the persistence configuration (located in the model module). It generates my war without returning errors but when it comes to instanciate the EntityManager, it throws a javax.persistence.PersistenceException: No Persistence provider for EntityManager named myPersistence

PARENT
|
|-DATABASE
| └ src/main/
| └ pom.xml
|
|-MODEL
| |- src/main
| |  └ java/
| |    └ .../JPAUtil.java
| |  └ ressources
| |    └ META-INF
| |      └ persistence.xml
| └ pom.xml
|
|-WS
| └ src/main/
| └ pom.xml
|
└ pom.xml

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">

    <persistence-unit name="myPersistence" >
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/mySchema" />
            <property name="javax.persistence.jdbc.user" value="user" />
            <property name="javax.persistence.jdbc.password" value="password" />
        </properties>
    </persistence-unit>

</persistence>

PARENT - pom.xml

<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>my.group.id</groupId>
    <artifactId>parent</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>modele</module>
        <module>database</module>
        <module>WS</module>
    </modules>

    <properties>
        <!--  encoding-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--java version -->
        <java-version>1.7</java-version>

        <!-- plugin versions -->
        <war-plugin-version>2.4</war-plugin-version>
        <compiler-plugin-version>3.1</compiler-plugin-version>

        <!-- dependency versions -->
        <javaee-api-version>7.0</javaee-api-version>

    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit-version}</version>
            </dependency>
            <dependency>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
                <version>2.0</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>5.3.1.Final</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>5.3.1.Final</version>
            </dependency>

            <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-json</artifactId>
                <version>1.8</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.37</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.9</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
            <dependency>
                <groupId>javax.persistence</groupId>
                <artifactId>javax.persistence-api</artifactId>
                <version>2.2</version>
            </dependency>

            <dependency>
                <groupId>my.group.id</groupId>
                <artifactId>database</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>my.group.id</groupId>
                <artifactId>modele</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>my.group.id</groupId>
                <artifactId>WS</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <!-- compiler plugin -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${compiler-plugin-version}</version>
                    <configuration>
                        <source>${java-version}</source>
                        <target>${java-version}</target>
                        <encoding>${project.build.sourceEncoding}</encoding>
                    </configuration>
                </plugin>

                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>${war-plugin-version}</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>

            </plugins>
        </pluginManagement>

    </build>

</project>

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

    <parent>
        <groupId>my.group.id</groupId>
        <artifactId>parent</artifactId>
        <version>1.0</version>
    </parent>
    <artifactId>modele</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
        </dependency>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

</project>

I have explored several solutions including:

  • changing persistence.xml file's location
  • changing the provider (org.hibernate.jpa.HibernatePersistenceProvider)
  • using c3p0 provider
  • including target/modele-1.0.jar into persistence.xml
  • including into persistence.xml
MWiesner
  • 7,913
  • 11
  • 31
  • 66
A. Gorin
  • 51
  • 1
  • 6

1 Answers1

1

I solved my problem: I was using Hibernate 5.3 with JPA 2.1. According to Hibernate documentation you can use either Hibernate 5.3 with JPA 2.2 or Hibernate 5.2 with JPA 2.1. So I migrated to Hibernate 5.2 and I no longer have this error.

A. Gorin
  • 51
  • 1
  • 6