Search code examples
javahibernategradleintellij-ideajava-14

Cannot resolve class 'HibernatePersistenceProvider'


I have the hibernate dependency in build.gradle but I'm getting the following error

> Task :JPABootstrapping.main() FAILED
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named recipes
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at setup.JPABootstrapping.main(JPABootstrapping.java:10)

Execution failed for task ':JPABootstrapping.main()'.
> Process 'command '/usr/lib/jvm/java-14-openjdk-amd64/bin/java'' finished with non-zero exit value 1

The persistence.xml is under src/main/resources/META-INF

<persistence>
    <persistence-unit name="recipes">
        <description>Hibernate Tips</description>
        <provider>
            org.hibernate.jpa.HibernatePersistenceProvider
        </provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL81Dialect" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://192.168.122.242:5432/recipes" />
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <property name="javax.persistence.jdbc.password" value="postgres" />
        </properties>
    </persistence-unit>
</persistence>

IntelliJ's inspection feature says HibernatePersistenceProvider cannot be resolved: enter image description here

The build.gradle is the following, contains the hibernate core and the postgresql driver dependencies:

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'org.hibernate', name: 'hibernate-core', version: '5.0.5.Final'
    implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.18'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Solution

  • That hibernate-core is really ancient, more precisely it was released in 2015. That artifact doesn't contain org.hibernate.jpa.HibernatePersistenceProvider class. Since then Hibernate has been renamed and refactored a lot. Latest stable is: implementation group: 'org.hibernate', name: 'hibernate-core', version: '5.4.26.Final' and this artifact has that class.

    Additional information: Hibernate will be renamed again. The new coordinates will: implementation group: 'org.hibernate.orm', name: 'hibernate-core', version: '6.0.0.Final'.