No Persistence provider for EntityManager named X
Asked Answered
S

6

17

I am developing a JavaSE application using JPA. Unfortunately, I get null after calling: Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

Below you will find:

  • A snippet of my code that invokes EntityManagerFactory and unexpectedly returns null
  • My persistence.xml file
  • My project structure

Snippet of my code:

public class Main {
    private static final String PERSISTENCE_UNIT_NAME = "MeineJpaPU";
    private static EntityManagerFactory factory;

    public static void main(String[] args) {
        // I get null on this line!!!
       factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

       EntityManager em = factory.createEntityManager();
       // do stuff with entity manager
       ...
    }
}

My persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                        http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="MeineJpaPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <class>path.to.package.server.Todo</class>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
     <properties>       
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>       
            <property name="javax.persistence.jdbc.url"  value="jdbc:postgresql://localhost:5432/test"/>       
            <property name="javax.persistence.jdbc.user" value="postgres"/>       
            <property name="javax.persistence.jdbc.password" value="postgres"/>       
        </properties>
  </persistence-unit>
</persistence>

My project structure:

This is the structure of my project:

Struve answered 11/10, 2013 at 16:20 Comment(0)
T
19

You must move persistence.xml file to an appropriate location.

More specifically, add META-INF/persistence.xml file to the root of a source folder.

In this case, the following is an appropriate location: src\main\java\META-INF\persistence.xml

Here are the details: (taken from the JPA spec)

A persistence.xml file defines a persistence unit. The persistence.xml file is located in the META-INF directory of the root of the persistence unit.

The root of the persistence unit is the key here.

If you are a non-Java EE app

The jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit.

If you are in a Java EE app, the following are valid

In Java EE environments, the root of a persistence unit must be one of the following:

  • an EJB-JAR file
  • the WEB-INF/classes directory of a WAR file[80]
  • a jar file in the WEB-INF/lib directory of a WAR file
  • a jar file in the EAR library directory
  • an application client jar file
Tita answered 11/10, 2013 at 21:10 Comment(1)
You actually saved me from my week-long tirade of trying to figure this xml stuff outHarder
E
12

Quick advice:

  • check if persistence.xml is in your classpath
  • check if hibernate provider is in your classpath

With using JPA in standalone application (outside of JavaEE), a persistence provider needs to be specified somewhere. This can be done in two ways that I know of:

In my case, I found out that due to maven misconfiguration, hibernate-entitymanager.jar was not included as a dependency, even if it was a transient dependency of other module.

See also answers here: No Persistence provider for EntityManager named

Eudora answered 6/3, 2014 at 12:30 Comment(1)
hibernate-entitymanager.jar is the correct answer for me too.Kathie
C
1

So in my case, everything was in the class path, but I had to add

Class c = Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");

I think this caused the PersistenceProvider to register itself with the javax classes. I have had to do something similar for JDBC drivers in the past as well.

Crayon answered 25/9, 2014 at 20:17 Comment(0)
A
1

I recently upgraded NB 8.1 to 8.2 and suddenly faced this problem and spent 2 days breaking my head to resolve. Up to 8.1, removing processorpath (mentioned above by others) worked. With 8.2, the problem persisted.

Finally, I found that the eclipselink.jar is missing in the default library of EclipseLink (JPA 2.1). I added the file to the definition of the library and voila - it started working!

Aeneus answered 23/12, 2016 at 1:31 Comment(0)
B
0

If you don't want to move your META-INF folder (maybe because that's where your IDE created it and you don't want to confuse it), and if you are using Maven, you can tell Maven where to look for META-INF using the <resources>tag. See: http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html

In your case:

<build>
  <resources>
    <resource>
      <directory>src</directory>
    </resource>
  </resources>
</build>
Borstal answered 15/12, 2016 at 8:24 Comment(0)
M
0

Dont give JPA dependency explicity

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

Thanks,
Rahul

Motorbus answered 20/9, 2017 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.