“没有EntityManager的持久性提供程序”错误

时间:2010-11-23 22:04:20

标签: java jpa maven

我是JPA的新手,我试图从书中做一个简单的例子。 但无论我做什么,我都会收到以下错误:

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>

我做错了什么? 提前谢谢。

6 个答案:

答案 0 :(得分:7)

JPA是由多个JPA提供程序(Hibernate,EclipseLink,OpenJPA,Toplink)实现的规范。

您需要选择要使用的提供程序,并为pom.xml添加适当的依赖项。您还需要在persistence.xml中指定您的提供商。

例如,如果您使用OpenJPA(我为此示例选择了它,因为其最新版本在Maven Central Repo中可用,因此无需配置特定于供应商的存储库):

 <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 :(得分:2)

如果您使用JPA + eclipselink Provider而不是使用此代码

<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 :(得分:2)

1)确保已定义持久性提供程序(适用于任何提供程序):    对于openjpa: <persistence-unit ...> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> ... ... </persistence-unit>

2)如果您使用自定义构建/编译过程(maven等)  确保将Meta-INF / persistance.xml复制到编译/类文件夹。

答案 3 :(得分:1)

实际上,

您似乎没有实际的Peristence Provider的依赖项。

JPA本身没有实现,您还需要使用Hibernate / Toplink / OpenJPA作为实际解决方案。

答案 4 :(得分:0)

根据你的persistence.xml,你使用TopLink和PostgreSQL作为RDBMS。当您在pom.xml中引用PostgreSQL JDBC驱动程序时,您尚未将TopLink声明为依赖项。

我的猜测(我承认)是持久性API在类路径中找不到TopLink(您的持久性提供程序)。尝试添加TopLink作为依赖项。

答案 5 :(得分:0)

作为规范的JPA具有多个提供者(实现者)。您必须选择一个为JPA规范提供实际字节码的方法。 Hibernate是常见的选择,但你的可能会有所不同。确定后,将其作为依赖项添加到POM中。否则,将JAR文件添加到构建路径。