2011-10-06 69 views
0

我正在使用MySql數據庫和JPA。我的persistence.xml看起來像Java持久性實體管理器工廠返回null

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" 
    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"> 
    <persistence-unit name="citheph"> 
     <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
     <class>persistence.citheph.pTerm</class> 
     <class>persistence.citheph.pPublicationterm</class> 
     <class>persistence.citheph.pPublicationauthor</class> 
     <class>persistence.citheph.pPublication</class> 
     <class>persistence.citheph.pCoauthorship</class> 
     <class>persistence.citheph.pCitation</class> 
     <class>persistence.citheph.pAuthor</class> 
     <properties> 
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/cithepth" /> 
      <property name="javax.persistence.jdbc.password" value="amer1" /> 
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> 
      <property name="javax.persistence.jdbc.user" value="root" /> 
     </properties> 

    </persistence-unit> 
</persistence> 
在我的Java代碼

我這樣做

EntityManagerFactory emf = Persistence.createEntityManagerFactory("citheph"); 
EntityManager em = emf.createEntityManager(); 

電動勢總會回來空。數據庫已連接並且ping成功。我甚至試圖將持久性單元名稱更改爲與persistance.xml中的條目不匹配,並且仍然爲空。它沒有拋出任何例外。當它到達代碼中的第二行時

EntityManager em = emf.createEntityManager(); 

我得到一個空指針異常。

任何幫助將不勝感激。

回答

0

打開最好的日誌記錄並確保沒有錯誤發生加載持久性單元。

你在使用什麼環境,你的classpath上是否有所有需要的jar?

+0

謝謝。我使用的是一個不同的庫,而不是eclips。感謝您的時間 – Amer

+0

你如何打開最好的日誌? –

0

您確定persistence.xml文件駐留在META-INF文件夾中嗎?

+0

是的。它在src/META-INF/ – Amer