JPA 2.0 using Hibernate as provider - Exception: No Persistence provider for EntityManager

80,138

Solution 1

Maybe you miss the Provider class or one of its dependencies in your pom.xml dependencies?

The link you give to the hibernate docs says that you should also add

<project ...>
  ...
  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>${hibernate-core-version}</version>
    </dependency>
  </dependencies>
</project>

to your pom.xml

Solution 2

persistence.xml is meant to be present in META-INF directory and META-INF is meant to be present in the classpath of the application which is src folder.

As per your folder structure its present in resource folder, try moving it to classpath it sholud work.

Share:
80,138

Related videos on Youtube

Holm
Author by

Holm

Updated on October 25, 2020

Comments

  • Holm
    Holm over 3 years

    I'm trying to set up a simple jpa 2.0 project by following the information in the Hibernate EntityManager documentation. I've been on this for hours now, but no matter what I do I always get this exception when I try to create a EntityManagerFactory:

    Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named manager1
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
        at se.mycomp.UserTest.main(UserTest.java:9)
    

    I've found quite a few similar questions regarding this exception, but no solutions that I am able to get to work. What am I doing wrong here?

    directory structure

    .
    ├── pom.xml
    └── src
        ├── main
        │   ├── java
        │   │   └── se
        │   │       └── mycomp
        │   │           ├── UserTest.java
        │   │           └── domain
        │   │               └── User.java
        │   └── resources
        │       ├── META-INF
        │       │   └── persistence.xml
        │       └── log4j.properties
        └── test
            └── java
    

    my persistence.xml

    <persistence 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_2_0.xsd"
                 version="2.0">
        <persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <class>se.mycomp.domain.User</class> 
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
                <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    
                <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
                <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test"/>
                <property name="javax.persistence.jdbc.user" value="test"/>
                <property name="javax.persistence.jdbc.password" value="1234"/>
            </properties>
        </persistence-unit>
    </persistence>
    

    my pom.xml

    <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">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>se.lil.tryjpa</groupId>
    <artifactId>try-jpa</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
        <hibernate-core.version>3.6.4.Final</hibernate-core.version>
        <mysql-connector-java.version>5.1.16</mysql-connector-java.version>
        <slf4j.version>1.6.1</slf4j.version>
        <log4j.version>1.6.1</log4j.version>
    </properties>
    
    <dependencies>
        <!-- HIBERNATE DEPENDENCIES -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate-core.version}</version>
        </dependency>
    
        <!-- MYSQL DEPENDENCIES -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector-java.version}</version>
        </dependency>
    
        <!-- Logging Dependencies -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <optimize>true</optimize>
                    <debug>true</debug>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    UserTest.java

    public class UserTest {
        public static void main(String[] args) {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");
            EntityManager em = emf.createEntityManager();
        }
    }
    
  • Martijn Verburg
    Martijn Verburg almost 11 years
    Not embarrassing at all, that error message is singularly useless (applies to a whole host of issues).
  • jFrenetic
    jFrenetic over 8 years
    I'd like to add that this dependency should replace the hibernate-core one, not just be added alongside, because it in fact already includes hibernate-core. See the list of Maven dependencies on Hibernate downloads page. They explicitly say that for JPA, use hibernate-entitymanager instead of hibernate-core.
  • Gonen I
    Gonen I about 8 years
    I was not using maven, so solved this by adding the hibernate-entity-manager.jar ( found in the dist\lib\optional\jpa directory in hibernate 5.5.2 zip ), and then by downloading and adding jta-1.1.jar
  • AnnetteC
    AnnetteC about 2 years
    hibernate-entitymanager is now integrated into hibernate-core