我根本无法解决这个问题,并且有大量的堆栈溢出帖子具有完全相同的错误消息。我已经尝试了所有这些,但仍然无法让它发挥作用。也许我看起来很小。我真的需要帮助,并提前感谢你。
错误消息
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
目录结构请忽略hibernate.cfg.xml文件,我尝试使用hibernate sessionFactory并遇到了一个我无法解决的重大错误,并说了它的地狱。此外post表示JPA是首选
这是persistence.xml文件
<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();
}
}
答案 0 :(得分:5)
您的持久性单元使用了错误的标识符。它的名称在这里指定:
...
<persistence-unit name="com.learning.jpa">
...
</persistence-unit>
</persistence>
StackTrace告诉你
没有名为 org.hibernate.ejb.HibernatePersistence
的EntityManager的持久性提供程序
所以你需要在相关的代码片段中通过正确的指定名称 com.learning.jpa 来引用PU,如下所示:
if (sessionFactory == null) {
sessionFactory = Persistence.createEntityManagerFactory("com.learning.jpa");
}
更改为正确的PU名称后,它应该有效。希望它有所帮助。