Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 730
Dalamar42 Avatar asked Feb 23 '14 22:02

Dalamar42


People also ask

What is javax persistence PersistenceException?

javax.persistenceThrown by the persistence provider when a problem occurs. All instances of PersistenceException except for instances of NoResultException , NonUniqueResultException , LockTimeoutException , and QueryTimeoutException will cause the current transaction, if one is active, to be marked for rollback.

Can't find any meta INF persistence xml file?

You need to move persistence. xml to a location on your app server's class path. For a Maven project, that is typically in the src/main/resources folder. Check the Hibernate documentation to see if Hibernate expects the file to be in a sub-folder, or if putting it in the root of the classpath is OK.

What is persistence provider in JPA?

Simply put, persistence provider refers to the specific JPA implementation used in our application to persist objects to the database. To learn more about JPA and its implementations, we can refer to our article on the difference between JPA, Hibernate, and EclipseLink.

What is persistence provider?

Persistence providers are implementations of the Java™ Persistence API (JPA) specification and can be deployed in the Java EE compliant application server that supports JPA persistence.


1 Answers

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.

like image 131
John Ament Avatar answered Sep 21 '22 19:09

John Ament