2013-10-11 64 views
12

我正在使用JPA開發JavaSE應用程序。不幸的是,我得到null打完電話後: Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);沒有EntityManager的持久性提供者名爲X

下面你會發現:

  • 調用EntityManagerFactory和意外的我的代碼片段返回null
  • persistence.xml文件
  • 我的項目結構

片段我的代碼:

public class Main { 
    private static final String PERSISTENCE_UNIT_NAME = "MeineJpaPU"; 
    private static EntityManagerFactory factory; 

    public static void main(String[] args) { 
     // I get null on this line!!! 
     factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); 

     EntityManager em = factory.createEntityManager(); 
     // do stuff with entity manager 
     ... 
    } 
} 

我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="1.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_1_0.xsd"> 
    <persistence-unit name="MeineJpaPU" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> 
    <class>path.to.package.server.Todo</class> 
     <exclude-unlisted-classes>false</exclude-unlisted-classes> 
    <properties>  
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>  
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/test"/>  
      <property name="javax.persistence.jdbc.user" value="postgres"/>  
      <property name="javax.persistence.jdbc.password" value="postgres"/>  
     </properties> 
    </persistence-unit> 
</persistence> 

我的項目結構:

This is the structure of my project:

回答

13

您必須persistence.xml文件移動到一個適當的位置。

更具體地說,將META-INF/persistence.xml文件添加到源文件夾的根目錄。

在這種情況下,下面是一個適當的位置:src\main\java\META-INF\persistence.xml

下面是詳細信息:

A persistence.xml file defines a persistence unit. The persistence.xml file is 
located in the META-INF directory of the root of the persistence unit. 

持久性單元的根(來自JPA規範截取)這裏是關鍵。

如果你是一個非Java EE應用程序

The jar file or directory whose META-INF directory contains the persistence.xml 
file is termed the root of the persistence unit. 

如果你是在一個的Java EE應用程序,以下是有效的

In Java EE environments, the root of a persistence unit must be one of the following: 
• an EJB-JAR file 
• the WEB-INF/classes directory of a WAR file[80] 
• a jar file in the WEB-INF/lib directory of a WAR file 
• a jar file in the EAR library directory 
• an application client jar file 
11

快速忠告:

  • 檢查persistence.xml是否在你的classpa中TH
  • 檢查,如果休眠提供商是在classpath

有了獨立的應用程序使用JPA(JavaEE的以外),持久性提供者需要在某處指定。

在我而言,這通常是包括在內,我發現,由於行家配置錯誤,hiberna te-entitymanager.jar未被包含爲依賴項,即使它是其他模塊的臨時依賴項。

參見答案在這裏:No Persistence provider for EntityManager named在我的情況

+2

休眠-entitymanager.jar是正確的答案對我來說太。 –

0

所以,一切都在類路徑,但我不得不添加

類C =的Class.forName(「org.eclipse.persistence.jpa .PersistenceProvider「);

我認爲這會導致PersistenceProvider向javax類註冊自己。過去我也必須爲JDBC驅動程序做類似的事情。

1

我最近升級NB 8.1至8.2和突然面對這個問題,了2天打破我的頭去解決。最多8.1,刪除processorpath(上面提到的其他人)工作。隨着8.2,問題依然存在。

最後,我發現EclipseLink(JPA 2.1)的默認庫中缺少eclipselink.jar。我將該文件添加到庫的定義中,並且瞧 - 它開始工作了!

0

不給JPA依賴明確地

 <dependency> 
        <groupId>javax.persistence</groupId> 
        <artifactId>persistence-api</artifactId> 
        <version>1.0.2</version>  
     </dependency> 

感謝,
拉胡爾

相關問題