Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

NPI – JPA Buddy – JPA (partner)
announcement - icon

JPA is huge! It covers nearly every aspect of communication between relational databases and the Java application and is deeply integrated into all major frameworks.

If you're using IntelliJ, JPA Buddy is super helpful. The plugin gently guides you through the subtleties of the most popular JPA implementations, visually reminds you of JPA features, generates code that follows best practices, and integrates intelligent inspections to improve your existing persistence code.

More concretely, it provides powerful tooling to generate Spring Data JPA repositories and methods, Flyway Versioned Migrations, Liquibase Differential Changelogs, DDL and SQL statements, DTO objects, and MapStruct interfaces.

Oh, and it actually generates JPA entities from an existing database and gradually update the data model as the database evolves! Yeah.

>> Become a lot more productive with JPA Buddy

1. Introduction

In this tutorial, we'll see how to solve a common Hibernate error — “No persistence provider for EntityManager”. 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.

2. What Causes the Error

We'll see the error when the application doesn't know which persistence provider should be used.

This occurs when the persistence provider is neither mentioned in the persistence.xml file nor configured in the PersistenceUnitInfo implementation class.

3. Fixing the Error

To fix this error, we simply need to define the persistence provider in the persistence.xml file:

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

Or, if we're using Hibernate version 4.2 or older:

<provider>org.hibernate.ejb.HibernatePersistence</provider>

In case we've implemented the PersistenceUnitInfo interface in our application, we must also override the
getPersistenceProviderClassName() method:

@Override
public String getPersistenceProviderClassName() {
    return HibernatePersistenceProvider.class.getName();
}

To ensure all the necessary Hibernate jars are available, it's important to add the hibernate-core dependency in the pom.xml file:

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

4. Conclusion

To summarize, we've seen the possible causes of the Hibernate error “No persistence provider for EntityManager” and various ways to solve it.

As usual, the sample Hibernate project is available over on GitHub.

Persistence bottom

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE
Persistence footer banner
Comments are closed on this article!