Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA + Hibernate = No Persistence provider for EntityManager

I'm trying to setup JPA for my Maven project, using Hibernate as provider.

Structure of the project

├── META-INF
│    └── persistence.xml
├── src
|   ├── main
|   |   └── java
|   |       ├── model
|   |       |   └── Instance.java
|   |       └── App.java
|   └── test
|       └── java
|           └── model
|               └── AppTest.java
└── pom.xml

Content of persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
    <persistence-unit name="testjpa" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>it.vitrociset.model.Instance</class>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/aquasystem"/>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
            <property name="hibernate.connection.username" value="username"/>
            <property name="hibernate.connection.password" value="password"/>
        </properties>
    </persistence-unit>
</persistence>

Content of Instance.java

package model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Instance {

    @Id
    private String id;
    private String path;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

}

Content of App.java

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class App {
    public static void main( String[] args ) {
        EntityManagerFactory entityManagerFactory =  Persistence.createEntityManagerFactory("testjpa");
        EntityManager em = entityManagerFactory.createEntityManager();
        EntityTransaction userTransaction = em.getTransaction();
        userTransaction.begin();
        Instance instance = new Instance();
        instance.setId("id");
        instance.setPath("path");
        em.persist(instance);
        userTransaction.commit();
        em.close();
        entityManagerFactory.close();
    }
}

Content of 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></groupId>
    <artifactId></artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.6.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.6.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>

    </dependencies>

</project>

I got the following exception:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named testjpa

but I have no idea why. What is wrong in my code? I wasn't able to find the solution.

like image 554
Andrea Avatar asked Oct 20 '14 15:10

Andrea


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 hibernate EntityManager?

The EntityManager API is used to access a database in a particular unit of work. It is used to create and remove persistent entity instances, to find entities by their primary key identity, and to query over all entities. This interface is similar to the Session in Hibernate. Persistence context.

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

Persistence.xml is not in resources directory and therefore not in classpath. Move META-INF directory to src/main/resources.

like image 105
Tomasz W Avatar answered Nov 15 '22 22:11

Tomasz W


Add hibernate-entitymanager.jar to the classpath to resolve this issue.

like image 35
Harrhy Saladagu Avatar answered Nov 15 '22 23:11

Harrhy Saladagu