Java “没有 EntityManager 的持久性提供程序”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4261435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"No Persistence provider for EntityManager" error
提问by Ivan Mushketyk
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:
我是 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)
I googled a lot and I did all that I read about JPA.
Here is a directory tree of my project:
我用谷歌搜索了很多,我做了我读到的关于 JPA 的所有内容。
这是我的项目的目录树:
.
|-- pom.xml
`-- src
|-- main
| |-- java
| | `-- com
| | `-- mycompany
| | `-- simpleentity
| | |-- Employee.java
| | |-- EmployeeService.java
| | `-- EmployeeTest.java
| `-- resources
| `-- META-INF
| `-- persistence.xml
`-- test
Here is my pom.xml:
这是我的 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>
Here is my source code: EmployeeTest.java:
这是我的源代码: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();
}
}
And here is my persistance.xml
这是我的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.
我做错了什么?先感谢您。
采纳答案by axtavt
JPA is a specification implemented by multiple JPA providers (Hibernate, EclipseLink, OpenJPA, Toplink).
JPA 是由多个 JPA 提供者(Hibernate、EclipseLink、OpenJPA、Toplink)实现的规范。
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
.
您需要选择要使用的提供程序并将适当的依赖项添加到您的pom.xml
. 您还需要在persistence.xml
.
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):
例如,如果您使用 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>
回答by willcodejavaforfood
Actually,
实际上,
You don't seem to have a dependency for an actual Peristence Provider.
您似乎对实际的 Peristence Provider 没有依赖性。
JPA in itself does not have a implementation, you will need to also use Hibernate/Toplink/OpenJPA as an actual solution.
JPA 本身没有实现,您还需要使用 Hibernate/Toplink/OpenJPA 作为实际解决方案。
回答by Florian
According to your persistence.xml you are using TopLink with PostgreSQL as the RDBMS. While you reference the PostgreSQL JDBC driver in your pom.xml, you haven't declared TopLink as a dependency.
根据您的persistence.xml,您使用TopLink 和PostgreSQL 作为RDBMS。当您在 pom.xml 中引用 PostgreSQL JDBC 驱动程序时,您尚未将 TopLink 声明为依赖项。
My guess (I admit) is that the persistence API doesn't find TopLink (your persistence provider) in the classpath. Try adding TopLink as a dependency.
我的猜测(我承认)是持久性 API 在类路径中找不到 TopLink(你的持久性提供者)。尝试添加 TopLink 作为依赖项。
回答by user1402963
if you use JPA + eclipselink Provider than use this code
如果您使用 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>
回答by MihaiS
1) make sure you have defined the persistence provider(for whatever provider):
Ex For openjpa:
<persistence-unit ...>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
...
...
</persistence-unit>
1)确保你已经定义了持久化提供者(对于任何提供者):Ex For openjpa:
<persistence-unit ...>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
...
...
</persistence-unit>
2) if you are using custom build/compile process (maven, etc) make sure your Meta-INF/persistance.xml is copied to the compiled/classes folder.
2) 如果您使用自定义构建/编译过程(maven 等),请确保将 Meta-INF/persistance.xml 复制到已编译/类文件夹中。
回答by Kingz
JPA being a specification has multiple providers (implementors). You have to choose one that provides the actual bytecode for the JPA specification. Hibernate is common choice, but yours may be different. Once you have identified that, add that as a dependency into your POM. Otherwise, add the JAR file to your build path.
JPA 作为一种规范有多个提供者(实现者)。您必须选择一种为 JPA 规范提供实际字节码的方法。Hibernate 是常见的选择,但您的选择可能有所不同。确定后,将其作为依赖项添加到 POM 中。否则,将 JAR 文件添加到您的构建路径。