2016-06-11 93 views
0

我根本無法解決這個問題,並且存在大量的堆棧溢出帖子,其中包含完全相同的錯誤消息。我已經嘗試過所有這些,但仍然無法實現。也許我看起來很小。我真的需要幫助,並提前感謝你。JPA錯誤 - 沒有EntityManager的持久性提供者名爲

錯誤消息

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最好 enter image description here

這裏有需要的jar文件 enter image description here

下面是

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

回答

5

您正在使用persistence.xml文件Persisten的錯誤標識符CE-單位。它的名字在這裏指定:

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

堆棧跟蹤告訴你

沒有持久性提供EntityManager的命名org.hibernate.ejb.HibernatePersistence

所以,你需要通過它是正確的參考PU,指定名稱com.learning.jpa在相關的代碼片段中是這樣的:

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

更改爲正確的PU名稱它應該工作。希望能幫助到你。

+1

謝謝你解決了這個問題。我之前嘗試過,實際上這是我最初的想法,但我一定忽略了錯誤,並認爲它是同一個錯誤,因爲這會產生新的錯誤。當我將項目從Eclipse移動到IntelliJ時,我沒有嘗試過。我要給出新的錯誤一個鏡頭,看看我能做什麼,這就是你有時候學習。 – Grim

相關問題