2010-11-23 76 views
5

我是JPA的新手,我試圖從書中做一個簡單的例子。 但無論我做什麼,我收到以下錯誤:「沒有EntityManager的持久性提供者」錯誤

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named EmployeeService 
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:89) 
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60) 
     at com.mycompany.simpleentity.EmployeeTest.main(EmployeeTest.java:18) 

我GOOGLE了很多,我做了所有我讀到JPA。
這裏是我的項目的目錄樹:

. 
|-- pom.xml 
`-- src 
    |-- main 
    | |-- java 
    | | `-- com 
    | |  `-- mycompany 
    | |   `-- simpleentity 
    | |    |-- Employee.java 
    | |    |-- EmployeeService.java 
    | |    `-- EmployeeTest.java 
    | `-- resources 
    |  `-- META-INF 
    |   `-- persistence.xml 
    `-- test 

這裏是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.mycompany</groupId> 
    <artifactId>SimpleEntity</artifactId> 
    <packaging>jar</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>SimpleEntity</name> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.persistence</groupId> 
     <artifactId>persistence-api</artifactId> 
     <version>1.0</version>  
    </dependency> 
    <dependency> 
     <groupId>postgresql</groupId> 
     <artifactId>postgresql</artifactId> 
     <version>9.0-801.jdbc4</version> 
    </dependency> 
    </dependencies> 

    <build> 

     <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
      <source>1.5</source> 
      <target>1.5</target> 
      </configuration> 
     </plugin> 

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 

      <configuration> 
       <archive> 
        <manifest> 
         <addClasspath>true</addClasspath> 
         <mainClass>com.mycompany.simpleentity.EmployeeTest</mainClass> 
         <!-- <classpathLayoutType>repository</classpathLayoutType> --> 
         <classpathMavenRepositoryLayout>true</classpathMavenRepositoryLayout> 
         <classpathPrefix>${env.HOME}/.m2/repository</classpathPrefix> 

        </manifest> 
       </archive> 
      </configuration> 
     </plugin> 
     </plugins> 

    </build> 
</project> 

這裏是我的源代碼: EmployeeTest.java:

package com.mycompany.simpleentity; 

import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.Persistence; 

public class EmployeeTest { 
    public static void main(String[] args) { 
     EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmployeeService"); 
     EntityManager em = emf.createEntityManager(); 

    } 
} 

這裏是我的persistance.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="EmployeeService" transaction-type="RESOURCE_LOCAL"> 
     <class>com.mycompany.simpleentity.Employee</class> 
     <properties> 
      <property name="toplink.jdbc.driver" 
         value="org.postgresql.Driver"/> 
      <property name="toplink.jdbc.url" 
         value="jdbc:postgresql://localhost:5432/testdb;create=true"/> 
      <property name="toplink.jdbc.user" value="postgres"/> 
      <property name="toplink.jdbc.password" value="111"/> 

     </properties> 
    </persistence-unit> 
</persistence> 

我該怎麼做? 預先感謝您。

回答

7

JPA是由多個JPA提供者(Hibernate,EclipseLink,OpenJPA,Toplink)實現的規範。

您需要選擇要使用的提供程序並將相應的依賴項添加到您的pom.xml。您還需要在persistence.xml中指定您的提供商。

例如,如果你使用OpenJPA的(我選用它的這個例子,因爲它的最新版本是Maven的中央回購可用的,所以沒有必要配置特定供應商的倉庫):

<dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <!-- Note that you don't need persistence-api dependency - it's transitive --> 
    <dependency> 
     <groupId>org.apache.openjpa</groupId> 
     <artifactId>openjpa-all</artifactId> 
     <version>2.0.1</version> 
    </dependency> 
    <dependency> 
     <groupId>postgresql</groupId> 
     <artifactId>postgresql</artifactId> 
     <version>9.0-801.jdbc4</version> 
    </dependency> 
    </dependencies> 

<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="EmployeeService" transaction-type="RESOURCE_LOCAL"> 
     <!-- Provider specification --> 
     <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> 

     <class>com.mycompany.simpleentity.Employee</class>  
     <properties>  
      <property name="javax.persistence.jdbc.driver"  
         value="org.postgresql.Driver"/>  
      <property name="javax.persistence.jdbc.url"  
         value="jdbc:postgresql://localhost:5432/testdb;create=true"/>  
      <property name="javax.persistence.jdbc.user" value="postgres"/>  
      <property name="javax.persistence.jdbc.password" value="111"/>  

     </properties>  
    </persistence-unit>  
</persistence> 
1

實際上,

您似乎沒有實際的Peristence Provider的依賴項。

JPA本身並沒有實現,您還需要使用Hibernate/Toplink/OpenJPA作爲實際解決方案。

0

根據您的persistence.xml,您將使用TopLink和PostgreSQL作爲RDBMS。當您在pom.xml中引用PostgreSQL JDBC驅動程序時,您尚未將TopLink聲明爲依賴項。

我的猜測(我承認)是持久性API在類路徑中找不到TopLink(您的持久性提供程序)。嘗試添加TopLink作爲依賴項。

2

如果你使用JPA +的EclipseLink提供比使用此代碼

<properties> 
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/Database name" /> 
      <property name="javax.persistence.jdbc.user" value="" /> 
      <property name="javax.persistence.jdbc.password" value="" /> 
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> 
</properties> 
2

1)請確保您已定義的持久性提供者(不管是什麼提供商): 防爆關於OpenJPA: <persistence-unit ...> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> ... ... </persistence-unit>

2)如果你正在使用自定義的編譯/編譯過程(maven等) 確保你的Meta-INF/persistance.xml被複制到編譯/ classes文件夾中。

+0

感謝您提出第二點。這是我的問題。謝謝!!! – nashuald 2013-03-25 14:53:29

0

JPA是一個規範有多個提供者(實現者)。您必須選擇一個爲JPA規範提供實際字節碼的程序。休眠是常見的選擇,但你可能會有所不同。一旦你確定了那個,把它作爲你的POM的一個依賴。否則,將JAR文件添加到您的構建路徑。

相關問題