Dalamar42
Dalamar42

Reputation: 107

javax.persistence.PersistenceException: No Persistence provider for EntityManager named

I am trying to setup persistence using JPA for a Java-EE project that I am currently a part of and I am running into numerous problems with the configuration. Currently, I have defined a RESOURCE_LOCAL persistence unit inside persistence.xml with the intention of using this one in unit testing and verifying everything else works before tackling the issue of setting up a JTA persistence unit and its corresponding JTA data source. However, even this "simple" case is causing errors.

In detail:

The error I am getting is this:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named kronosTestLocal
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at persistence.EntityManagerTest.getStaff(EntityManagerTest.java:56)
    at persistence.EntityManagerTest.setup(EntityManagerTest.java:17)
    ...

The code snippet that throws the error is the following inside a JUnit test:

private final String persistenceUnitName = "kronosTestLocal";
....
final EntityManagerFactory entityManagerFactory =
    Persistence.createEntityManagerFactory(persistenceUnitName);

persistence.xml (inside META-INF and declared in the intellij JPA facet): (I tried removing the kronos persistence unit in case it was interfering somehow, but that didn't help)

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="kronos" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>jdbc/kronos-database</jta-data-source>

        <class>persistence.entities.DaySlotsEntity</class>
        <class>persistence.entities.DaysEntity</class>
        <class>persistence.entities.EmailNotificationsEntity</class>
        <class>persistence.entities.EmailSendingTimestampsEntity</class>
        <class>persistence.entities.GroupSlotsEntity</class>
        <class>persistence.entities.GroupsEntity</class>
        <class>persistence.entities.InterviewerEntity</class>
        <class>persistence.entities.InterviewerPreferencesEntity</class>
        <class>persistence.entities.InterviewerUnavailableDaysEntity</class>
        <class>persistence.entities.InterviewerWorkloadEntity</class>
        <class>persistence.entities.InterviewsEntity</class>
        <class>persistence.entities.PreferenceTypesEntity</class>
        <class>persistence.entities.PreferencesEntity</class>
        <class>persistence.entities.StaffEntity</class>
        <class>persistence.entities.StudentsEntity</class>
        <class>persistence.entities.SwapsEntity</class>
    </persistence-unit>

    <persistence-unit name="kronosTestLocal" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <class>persistence.entities.DaySlotsEntity</class>
        <class>persistence.entities.DaysEntity</class>
        <class>persistence.entities.EmailNotificationsEntity</class>
        <class>persistence.entities.EmailSendingTimestampsEntity</class>
        <class>persistence.entities.GroupSlotsEntity</class>
        <class>persistence.entities.GroupsEntity</class>
        <class>persistence.entities.InterviewerEntity</class>
        <class>persistence.entities.InterviewerPreferencesEntity</class>
        <class>persistence.entities.InterviewerUnavailableDaysEntity</class>
        <class>persistence.entities.InterviewerWorkloadEntity</class>
        <class>persistence.entities.InterviewsEntity</class>
        <class>persistence.entities.PreferenceTypesEntity</class>
        <class>persistence.entities.PreferencesEntity</class>
        <class>persistence.entities.StaffEntity</class>
        <class>persistence.entities.StudentsEntity</class>
        <class>persistence.entities.SwapsEntity</class>

        <properties>
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/kronos"/>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
            <property name="hibernate.connection.username" value="postgres"/>
            <property name="hibernate.connection.password" value="****"/>
            <property name="show_sql" value="true"/>
            <property name="hibernate.dialect"  value="org.hibernate.dialect.PostgreSQLDialect"/>
        </properties>
    </persistence-unit>
</persistence>

Maven 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">
    <parent>
        <artifactId>kronos</artifactId>
        <groupId>kronos</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ejb</artifactId>

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

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.2-1003-jdbc4</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.6</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <ejbVersion>3.2</ejbVersion>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

From my research I have seen a lot of people experiencing similar problems, but from what I can tell I am not doing any of the common mistakes. A few of us have been trying to tackle this problem for a while now so any help would be welcome.

Upvotes: 5

Views: 5006

Answers (1)

John Ament
John Ament

Reputation: 11723

If you're doing this from a JUnit test, and using maven, the persistence.xml should be located in src/test/resources/META-INF/persistence.xml which will be put into the correct location at test execution time. The file in src/main/resources/META-INF/ is not used as it is not in the test-jar's path.

Upvotes: 10

Related Questions