Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Persistence provider for EntityManager named X

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:

like image 417
joshi737 Avatar asked Oct 11 '13 16:10

joshi737


People also ask

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 a 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.

How do I import persistence to Jakarta?

Open the Database tool window by going to View -> Tool Windows -> Database. Click on the + button and choose Data source from URL. Then, paste in the database URL specified in the persistence. xml file ( jdbc:hsqldb:file:target/myDB;shutdown=true ).


2 Answers

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
like image 178
cmd Avatar answered Sep 21 '22 17:09

cmd


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:

  • either add provider element into the persistence unit: <provider>org.hibernate.ejb.HibernatePersistence</provider> (as described in correct answer by Chris here: https://stackoverflow.com/a/1285436/784594)
  • or provider for interface javax.persistence.spi.PersistenceProvider must be specified as a service, see here: http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html (this is usually included when you include hibernate,or another JPA implementation, into your classpath

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

like image 45
OndroMih Avatar answered Sep 22 '22 17:09

OndroMih