java.lang.ClassNotFoundException:无法使用 JPA 找到 javax.persistence.Persistence
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25639515/
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
java.lang.ClassNotFoundException: javax.persistence.Persistence cannot be found with JPA
提问by gamo
I'm praticing with JPA API. I got an error
我正在使用 JPA API 进行练习。我有一个错误
java.lang.ClassNotFoundException: javax.persistence.Persistence cannot be found
My code below:
我的代码如下:
EntityManagerFactory emf;
emf = Persistence.createEntityManagerFactory("mail");
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("SELECT v FROM Version v");
List<Version> versions = query.getResultList();
The error at line emf = Persistence.createEntityManagerFactory("mail");
线上的错误 emf = Persistence.createEntityManagerFactory("mail");
Any solution?
有什么解决办法吗?
回答by zbig
You are trying to set up a standalone JPA project. In order to do so you need a JPA provider jars. The two more popular providers are Eclipselink and Hibernate. If you are using maven you can add dependencies to their implementations.
您正在尝试建立一个独立的 JPA 项目。为此,您需要一个 JPA 提供程序 jar。两个比较流行的提供程序是 Eclipselink 和 Hibernate。如果您使用的是 maven,您可以向它们的实现添加依赖项。
For Eclipselink
<dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>eclipselink</artifactId> <version>2.5.1</version> </dependency>
For Hibernate
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.6.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.6.Final</version> </dependency>
对于 Eclipselink
<dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>eclipselink</artifactId> <version>2.5.1</version> </dependency>
对于休眠
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.6.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.6.Final</version> </dependency>
If you are not using maven you can download their implementations from their sites and put it in your classpath.
如果您不使用 maven,您可以从他们的站点下载他们的实现并将其放在您的类路径中。
- For Eclipselink: http://www.eclipse.org/eclipselink/downloads/
- For Hibernate: http://hibernate.org/orm/
- 对于 Eclipselink:http: //www.eclipse.org/eclipselink/downloads/
- 对于休眠:http: //hibernate.org/orm/
Some JPA quickstarts are recommending to add only the JPA API (interface declarations only) dependencies with maven.
一些 JPA 快速入门建议仅添加与 maven 的 JPA API(仅限接口声明)依赖项。
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
or
或者
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
This approach will be successful only in server environment as the server will provide appropriate implementation at runtime.
这种方法只会在服务器环境中成功,因为服务器将在运行时提供适当的实现。
回答by Atul Jain
Right Click on Project -> Properties -> Search "Deployment Assembly" -> Add Maven Dependencies.
右键单击项目-> 属性-> 搜索“部署程序集”-> 添加Maven 依赖项。