簡體   English   中英

javax.persistence.PersistenceException:沒有名為customerManager的EntityManager的持久性提供程序

[英]javax.persistence.PersistenceException: No Persistence provider for EntityManager named customerManager

我是JPA和Hibernate的新手。 在閱讀了一些在線資料后,我現在了解了Hibernate是什么以及它如何與JPA一起使用。

現在,我正在嘗試運行這個JPA和Hibernate教程 我已經完成了他們在本教程中提到的所有內容。

我沒有Oracle DB,只有MySQL。 所以我使用我對JPA和Hibernate的理解對persistence.xml做了一些更改(我不知道它是否正確......對我而言似乎是。)

這是我的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_1_0.xsd" version="1.0">
  <persistence-unit name="customerManager" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>Customer</class>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.connection.username" value="root"/>
      <property name="hibernate.connection.password" value="1234"/>
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/general"/>
      <property name="hibernate.max_fetch_depth" value="3"/>
    </properties>
  </persistence-unit>
</persistence>

但我似乎沒有得到他們描述的輸出。 它給了我:

Customer id before creation:null
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence     provider for EntityManager named customerManager
 at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
 at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
 at CustomerDAO.create(CustomerDAO.java:8)
 at CustomerDAO.main(CustomerDAO.java:22)

任何建議將不勝感激。

更新:

我已經做了要求完成的更改。 但是,仍然得到asme錯誤行!

他們沒有在該教程中提到有關orm.xml的任何內容。 這可能是一個問題!

只是為了完整。 還有另一種情況導致此錯誤:

缺少META-INF / services / javax.persistence.spi.PersistenceProvider文件。

對於Hibernate,它位於hibernate-entitymanager-XXX.jar ,因此,如果hibernate-entitymanager-XXX.jar不在您的類路徑中,您也會遇到此錯誤。

此錯誤消息是如此誤導,並且它花費我幾個小時才能使其正確。

請參閱使用Hibernate作為提供程序的JPA 2.0 - 例外:EntityManager沒有持久性提供程序

您的persistence.xml無效,無法創建EntityManagerFactory 它應該是:

<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_1_0.xsd" version="1.0">
  <persistence-unit name="customerManager" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>Customer</class>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.connection.username" value="root"/>
      <property name="hibernate.connection.password" value="1234"/>
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/general"/>
      <property name="hibernate.max_fetch_depth" value="3"/>
    </properties>
  </persistence-unit>
</persistence>

(注意<property>元素是如何關閉的,它們不應該嵌套)

更新:我完成了教程,您還必須在使用MySQL時更改Id生成策略(因為MySQL不支持序列)。 我建議使用AUTO策略(默認為IDENTITY和MySQL)。 為此,請刪除SequenceGenerator注釋並更改如下代碼:

@Entity
@Table(name="TAB_CUSTOMER")
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="CUSTOMER_ID", precision=0)
    private Long customerId = null;

   ...
}

這應該有所幫助。

PS:您還應該按照建議提供log4j.properties

我今天遇到了同樣的問題。 我的persistence.xml位於錯誤的位置。 我不得不把它放在以下路徑中:

project/src/main/resources/META-INF/persistence.xml

我面臨同樣的問題。 我意識到我在persistence.xml中使用了錯誤的提供程序類

對於Hibernate應該是

<provider>org.hibernate.ejb.HibernatePersistence</provider>

對於EclipseLink應該是

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

如果你使用Hibernate 5.2.10.Final,你應該改變

<provider>org.hibernate.ejb.HibernatePersistence</provider>

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

在persistence.xml中

根據Hibernate 5.2.2:EntityManager沒有持久性提供程序

如果您使用的是Maven,則可能同時包含 src/{main,test}/resources/META-INF/persistence.xml 這是一個常見的設置:使用h2或Derby測試您的JPA代碼,並使用PostgreSQL或其他一些完整的DBMS進行部署。 如果您正在使用此模式,請確保這兩個文件具有不同的單元名稱,否則Persistence類的某些版本將嘗試加載BOTH(因為您的測試時間CLASSPATH包括類和測試類); 這將導致持久性單元的定義沖突,導致我們都討厭的可怕消息!

更糟糕的是:這可能會與某些舊版本(如Hibernate)“兼容”,但在當前版本中失敗。 無論如何都值得一試......

有點太晚了,但是我遇到了同樣的問題並修復了它在persistence.xml文件(第1行) 中將schemalocation切換到schemaLocation

我已經看到了這個錯誤,對我來說問題是persistance.xml的絕對路徑中有一個空格,刪除它同樣幫助了我。

當我試圖在Tomcat 8中配置JPA實體管理器時,我也遇到了同樣的問題。首先,我遇到了SystemException類未找到的問題,因此沒有創建entityManagerFactory。 我刪除了hibernate實體管理器依賴項,然后我的entityManagerFactory無法查找持久性提供程序。 經過大量的研究和時間后,我們必須知道hibernate實體管理器必須查找一些配置。 然后放回實體管理器jar,然后添加JTA Api作為依賴項,它工作正常。

我的經驗告訴我,缺少persistence.xml,也會生成相同的異常。

我今天遇到了同樣的錯誤信息,當我試圖運行由螞蟻包裝的jar包時。

當我使用jar tvf來檢查jar文件的內容時,我意識到“ant”忘了為我包裝persistnece.xml。

手動重新打包jar文件后,錯誤信息消失了。

所以我相信也許你應該嘗試簡單地將META-INF放在src目錄下並將你的persistence.xml放在那里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM