“没有用于EntityManager的持久性提供程序"错误 [英] "No Persistence provider for EntityManager" error

查看:73
本文介绍了“没有用于EntityManager的持久性提供程序"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am newbie to JPA and I tried to do a simple example from the book. But no matter what I do I receive following error:

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的所有读物.
这是我的项目的目录树:

I googled a lot and I did all that I read about JPA.
Here is a directory tree of my project:

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

这是我的pom.xml:

Here is my 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:

Here is my source code: 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

And here is my 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>

我做错了什么? 预先谢谢你.

What do I do wrong? Thank you in advance.

推荐答案

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

JPA is a specification implemented by multiple JPA providers (Hibernate, EclipseLink, OpenJPA, Toplink).

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

You need to choose a provider to use and add the appropriate dependency to your pom.xml. Also you need to specify your provider in persistence.xml.

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

For example, if you use OpenJPA (I choosed it for this example since its latest version is available in Maven Central Repo, so there is no need to configure vendor-specific repositories):

 <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>  

这篇关于“没有用于EntityManager的持久性提供程序"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆