No Persistence provider for EntityManager named

Viewed 434614

I have my persistence.xml with the same name using TopLink under the META-INF directory. Then, I have my code calling it with:

EntityManagerFactory emfdb = Persistence.createEntityManagerFactory("agisdb");

Yet, I got the following error message:

2009-07-21 09:22:41,018 [main] ERROR - No Persistence provider for EntityManager named agisdb
javax.persistence.PersistenceException: No Persistence provider for EntityManager named agisdb
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:89)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)

Here is the persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="agisdb">
        <class>com.agis.livedb.domain.AddressEntity</class>
        <class>com.agis.livedb.domain.TrafficCameraEntity</class>
        <class>com.agis.livedb.domain.TrafficPhotoEntity</class>
        <class>com.agis.livedb.domain.TrafficReportEntity</class>
        <properties>
            <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/agisdb"/>
            <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="toplink.jdbc.user" value="root"/>
            <property name="toplink.jdbc.password" value="password"/>
        </properties>
    </persistence-unit>
</persistence>

It should have been in the classpath. Yet, I got the above error.

32 Answers

I'm some years late to the party here but I hit the same exception while trying to get Hibernate 3.5.1 working with HSQLDB and a desktop JavaFX program. I got it to work with the help of this thread and a lot of trial and error. It seems you get this error for a whole variety of problems:

No Persistence provider for EntityManager named mick

I tried building the hibernate tutorial examples but because I was using Java 10 I wasn't able to get them to build and run easily. I gave up on that, not really wanting to waste time fixing its problems. Setting up a module-info.java file (Jigsaw) is another hairball many people haven't discovered yet.

Somewhat confusing is that these (below) were the only two files I needed in my build.gradle file. The Hibernate documentation isn't clear about exactly which Jars you need to include. Entity-manager was causing confusion and is no longer required in the latest Hibernate version, and neither is javax.persistence-api. Note, I'm using Java 10 here so I had to include the jaxb-api, to get around some xml-bind errors, as well as add an entry for the java persistence module in my module-info.java file.

Build.gradle

// https://mvnrepository.com/artifact/org.hibernate/hibernate-core
compile('org.hibernate:hibernate-core:5.3.1.Final')

// https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'

Module-info.java

// Used for HsqlDB - add the hibernate-core jar to build.gradle too
requires java.persistence;

With hibernate 5.3.1 you don't need to specify the provider, below, in your persistence.xml file. If one is not provided the Hibernate provider is chosen by default.

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

The persistence.xml file should be located in the correct directory so:

src/main/resources/META-INF/persistence.xml

Stepping through the hibernate source code in the Intellij debugger, where it checks for a dialect, also threw the exact same exception, because of a missing dialect property in the persistence.xml file. I added this (add the correct one for your DB type):

<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>

I still got the same exception after this, so stepping through the debugger again in Intellij revealed the test entity I was trying to persist (simple parent-child example) had missing annotations for the OneToMany, ManyToOne relationships. I fixed this and the exception went away and my entities were persisted ok.

Here's my full final persistence.xml:

<persistence 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"
         version="2.1">

<persistence-unit name="mick" transaction-type="RESOURCE_LOCAL">
    <description>
        Persistence unit for the JPA tutorial of the Hibernate Getting Started Guide
    </description>

    <!-- Provided in latest release of hibernate
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    -->

    <class>com.micks.scenebuilderdemo.database.Parent</class>
    <class>com.micks.scenebuilderdemo.database.Child</class>

    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbc.JDBCDriver"/>

        <property name="javax.persistence.jdbc.url"
                  value="jdbc:hsqldb:file:./database/database;DB_CLOSE_DELAY=-1;MVCC=TRUE"/>

        <property name="javax.persistence.jdbc.user" value="sa"/>
        <property name="javax.persistence.jdbc.password" value=""/>

        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
    </properties>

</persistence-unit>

</persistence>

I probably wasted about half a day on this gem. My advice would be to start very simple - a single test entity with one or two fields, as it seems like this exception can have many causes.

In my case, previously I use idea to generate entity by database schema, and the persistence.xml is automatically generated in src/main/java/META-INF,and according to https://stackoverflow.com/a/23890419/10701129, I move it to src/main/resources/META-INF, also marked META-INF as source root. It works for me.

But just simply marking original META-INF(that is, src/main/java/META-INF) as source root, doesn't work, which confuses me.

and this is the structre: enter image description here

Mine got resolved by adding info in persistence.xml e.g. <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> and then making sure you have the library on classpath e.g. in Maven add dependency like

<dependency>
  <groupId>org.eclipse.persistence</groupId>
  <artifactId>eclipselink</artifactId>
  <version>2.5.0</version>
</dependency>

Verify the peristent unit name

<persistence-unit name="com.myapp.model.jpa"
    transaction-type="RESOURCE_LOCAL">    
public static final String PERSISTENCE_UNIT_NAME = "com.myapp.model.jpa";
Persistence.createEntityManagerFactory(**PERSISTENCE_UNIT_NAME**);

In my case it was about mistake in two properties as below. When I changed them ‘No Persistence provider for EntityManager named’ disappered.

So you could try test connection with your properties to check if everything is correct.

<property name="javax.persistence.jdbc.url" value="...”/>
<property name="javax.persistence.jdbc.password" value="...”/>

Strange error, I was totally confused because of it.

Try also copying the persistence.xml manually to the folder <project root>\bin\META-INF. This fixed the problem in Eclipse Neon with EclipseLink 2.5.2 using a simple plug-in project.

Had the same issue, but this actually worked for me :

mvn install -e  -Dmaven.repo.local=$WORKSPACE/.repository.

NB : The maven command above will reinstall all your project dependencies from scratch. Your console will be loaded with verbose logs due to the network request maven is making.

You have to use the absolute path of the file otherwise this will not work. Then with that path we build the file and pass it to the configuration.

@Throws(HibernateException::class)
fun getSessionFactory() : SessionFactory {
    return Configuration()
                .configure(getFile())
                .buildSessionFactory()
    }


private fun getFile(canonicalName: String): File {
    val absolutePathCurrentModule = System.getProperty("user.dir")
    val pathFromProjectRoot = absolutePathCurrentModule.dropLastWhile { it != '/' }
    val absolutePathFromProjectRoot = "${pathFromProjectRoot}module-name/src/main/resources/$canonicalName"
    println("Absolute Path of secret-hibernate.cfg.xml: $absolutePathFromProjectRoot")
    return File(absolutePathFromProjectRoot)
}

GL

Source

I had the same problem, I removed "@ManagedBean" from my bean class now working.

I ran into this problem when I moved all my java classes to a different package. Finally, I figured out that if I change the packages of myclasses, then I need to update persistence.xml with the new packages in all the persistence-unit/class elements that refer to my java classes.

Related