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
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();
}
}