Andrea Andrea - 1 year ago 219
Java Question

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

│ └── persistence.xml
├── src
| ├── main
| | └── java
| | ├── model
| | | └──
| | └──
| └── test
| └── java
| └── model
| └──
└── pom.xml

Content of persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="" version="2.1">
<persistence-unit name="testjpa" transaction-type="RESOURCE_LOCAL">
<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"/>

Content of

package model;

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

public class Instance {

private String id;
private String path;

public String getId() {
return id;

public void setId(String id) { = id;

public String getPath() {
return path;

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


Content of

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();
Instance instance = new Instance();

Content of pom.xml

<project xmlns="" xmlns:xsi=""











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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download