0

I can't solve this problem at all and there are a ton of stack overflow posts with this exact same error message. I have tried them all and I still can't get it to work. Maybe I am over looking something small. I really need help with this and thank you in advance.

Error Message

Initial EntityManagerFactory creation failed.javax.persistence.PersistenceException: No Persistence provider for EntityManager named org.hibernate.ejb.HibernatePersistence
Exception in thread "main" java.lang.ExceptionInInitializerError
    at util.DatabaseUtil.buildSessionFactory(DatabaseUtil.java:18)
    at util.DatabaseUtil.<clinit>(DatabaseUtil.java:8)
    at Test.main(Test.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named org.hibernate.ejb.HibernatePersistence
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at util.DatabaseUtil.buildSessionFactory(DatabaseUtil.java:13)
    ... 7 more

Directory Structure please ignore the hibernate.cfg.xml file, I tried using hibernate sessionFactory and ran into a major error I couldn't solve as well and said the hell with it. Plus this post says JPA is preferred enter image description here

Here are the needed jar files enter image description here

Here is the persistence.xml file

<persistence 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"
         version="2.0">

  <persistence-unit name="com.learning.jpa">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <class>com.learning.jpa.entities.User</class>
    <class>com.learning.jpa.entities.Expense</class>
    <class>com.learning.jpa.entities.Category</class>

    <properties>
        <property name="hibernate.connection.pool_size" value="1"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/budget_calculator"/>
        <property name="hibernate.onnection.username" value="root"/>
        <property name="hibernate.connection.password" value="password"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>    
    </properties>        
  </persistence-unit>

</persistence>

EntityManagerFactory

public class DatabaseUtil {

    private static EntityManagerFactory sessionFactory = buildSessionFactory();

    private static EntityManagerFactory buildSessionFactory() {
        try {
            if (sessionFactory == null) {
                sessionFactory = Persistence.createEntityManagerFactory("org.hibernate.ejb.HibernatePersistence");
            }
            return sessionFactory;
        } catch (Throwable ex) {
            System.err.println("Initial EntityManagerFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static EntityManagerFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        getSessionFactory().close();
    }
}
Community
  • 1
  • 1
Grim
  • 2,398
  • 4
  • 35
  • 54

1 Answers1

6

You are using the wrong identifier for your Persistence-Unit. The name of it is specified here:

...
  <persistence-unit name="com.learning.jpa">
  ...
  </persistence-unit>
</persistence>

The StackTrace tells you

No Persistence provider for EntityManager named org.hibernate.ejb.HibernatePersistence

So you need to reference the PU via its correct, given name com.learning.jpa in the relevant code snippet like so:

if (sessionFactory == null) {
    sessionFactory = Persistence.createEntityManagerFactory("com.learning.jpa"); 
}

Having changed to the correct PU-name it should work. Hope it helps.

MWiesner
  • 8,868
  • 11
  • 36
  • 70
  • 1
    thank you this fixed the problem. I tried this before, actually this is what I had initially but I must have overlooked the error and thought it was the same error because this generates a new error. I didn't try this when I moved the project to IntelliJ from Eclipse. Im going to give the new error a shot and see what I can do, thats how you learn sometimes. – Grim Jun 12 '16 at 01:34