pawel
pawel

Reputation: 6136

Using Hibernate with Gradle - no persistence provider for EntityManager named

Im trying to make Hibernate working with my Gradle project, but all I got is Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider

for EntityManager named manago
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
    at jpa.EntityManagerFactory.reinit(EntityManagerFactory.java:69)
    at jpa.EntityManagerFactory.<init>(EntityManagerFactory.java:61)
    at jpa.EntityManagerFactory.<init>(EntityManagerFactory.java:56)
    at jpa.EntityManagerFactory.getInstance(EntityManagerFactory.java:43)
    at main.Main.main(Main.java:32)
:run FAILED

my build.gradle file:

apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'war'
mainClassName = "main.Main"
sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDir
sourceCompatibility = '1.6'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'main.Main'
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '3.6.7.Final'
    compile 'postgresql:postgresql:9.1-901-1.jdbc4'
    compile "javax.ws.rs:jsr311-api:1.1.1"
    compile 'com.sun.jersey:jersey-server:1.13'
    compile 'com.sun.jersey:jersey-core:1.13'
    compile 'com.sun.jersey:jersey-servlet:1.13'
    testCompile group: 'junit', name: 'junit', version: '4.10'
    runtime group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    runtime group: 'org.hibernate', name: 'hibernate-entitymanager', version: '3.6.7.Final'
}

persistence.xml file in /src/META-INF directory:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
<persistence-unit name="WebSISMSPUpgsql" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>jpa.Routes</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
        <property name="hibernate.connection.username" value="postgres"/>
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
        <property name="hibernate.connection.password" value="postgres"/>
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postgres"/>
        <property name="hibernate.ejb.event.post-insert" value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener"/>
        <property name="hibernate.ejb.event.post-update" value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener"/>
        <property name="hibernate.ejb.event.post-delete" value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener"/>
        <property name="hibernate.ejb.event.pre-collection-update" value="org.hibernate.envers.event.AuditEventListener"/>
        <property name="hibernate.ejb.event.pre-collection-remove" value="org.hibernate.envers.event.AuditEventListener"/>
        <property name="hibernate.ejb.event.post-collection-recreate" value="org.hibernate.envers.event.AuditEventListener"/>
    </properties>
</persistence-unit>
</persistence>

Main class:

package main;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import javax.persistence.Persistence;
import jpa.Routes;
import org.hibernate.Session;


public class Main {
   public static void main(String[] args) {
               EntityManagerFactory emf = Persistence.createEntityManagerFactory("manago");
    }
}

What should I do to finally get it to work?

Upvotes: 1

Views: 5442

Answers (2)

SudheerKancharla
SudheerKancharla

Reputation: 3

Add below provider into your persistence.xml and try

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

Upvotes: 0

wh81752
wh81752

Reputation: 916

Your persistence-unit is - rather clearly - named as WebSISMSPUpgsql in your persistence.xml and not manago. Simply change Main.main into

public static void main(String[] args) {
 EntityManagerFactory emf = \
    Persistence.createEntityManagerFactory("WebSISMSPUpgsql");
}

or change the unit's name in persistence.xml.

Upvotes: 2

Related Questions