eclipse 导入 javax.persistence 无法解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15598210/
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
The import javax.persistence cannot be resolved
提问by Matt
I'm currently working on a project that requires EntityManager EntityManagerFacotry and Persistence each from the javax.persistence package. It seems to be for the database service, but the current code is not very well documented. By searching google it seems that there should be an xml file that comes along with this, but there isn't one of those either. I guess my question is simply how do I make these unresolved imports go away? Do I have to add another jar to the build path? It seems that I shouldn't have to since it's been around since 1.5.
我目前正在开发一个需要 EntityManager EntityManagerFacotry 和 Persistence 的项目,每个项目都来自 javax.persistence 包。它似乎是针对数据库服务的,但是当前的代码没有很好的文档记录。通过搜索谷歌,似乎应该有一个伴随着这个的 xml 文件,但也没有一个。我想我的问题只是如何让这些未解决的导入消失?我是否必须在构建路径中添加另一个 jar?似乎我不应该这样做,因为它从 1.5 开始就已经存在了。
Any help is greatly appreciated.
任何帮助是极大的赞赏。
采纳答案by Karthik Reddy
hibernate-distribution-3.6.10.Final\lib\jpa : Add this jar to solve the issue. It is present in lib folder inside that you have a folder called jpa ---> inside that you have hibernate-jpa-2.0-1.0.1.Final jar
hibernate-distribution-3.6.10.Final\lib\jpa :添加这个 jar 来解决问题。它存在于 lib 文件夹中,您有一个名为 jpa 的文件夹 ---> 里面有 hibernate-jpa-2.0-1.0.1.Final jar
回答by typoerrpr
Yes, you will likely need to add another jar or dependency
是的,您可能需要添加另一个 jar 或依赖项
javax.persistence.*
is part of the Java Persistence API (JPA). It is only an API, you can think of it as similar to an interface. There are many implementations of JPA and this answergives a very good elaboration of each, as well as which to use.
javax.persistence.*
是 Java Persistence API ( JPA) 的一部分。它只是一个 API,您可以将其视为类似于接口。JPA 有很多实现,这个答案很好地阐述了每个实现以及使用哪个。
If your javax.persistence.*
import cannot be resolved, you will need to provide the jar that implements JPA. You can do that either by manually downloading it (and adding it to your project) or by adding a declaration to a dependency management tool (for eg, Ivy/Maven/Gradle). See herefor the EclipseLink implementation (the reference implementation) on Maven repo.
如果您的javax.persistence.*
导入无法解析,您将需要提供实现 JPA 的 jar。您可以通过手动下载它(并将其添加到您的项目)或通过向依赖项管理工具(例如,Ivy/Maven/Gradle)添加声明来实现。有关Maven 存储库上的 EclipseLink 实现(参考实现),请参见此处。
After doing that, your imports should be resolved.
这样做之后,您的导入应该得到解决。
Also see herefor what is JPA about. The xml you are referring to could be persistence.xml
, which is explained on page 3 of the link.
另请参阅此处了解 JPA 的内容。您所指的 xml 可能是persistence.xml
,这在链接的第 3 页上进行了解释。
That being said, you might just be pointing to the wrong target runtime
话虽如此,您可能只是指向错误的目标运行时
If i recall correctly, you don't need to provide a JPA implementation if you are deploying it into a JavaEE app server like JBoss. See here"Note that you typically don't need it when you deploy your application in a Java EE 6 application server (like JBoss AS 6 for example).". Try changing your project's target runtime.
如果我没记错的话,如果您将 JPA 部署到像 JBoss 这样的 JavaEE 应用服务器中,则不需要提供 JPA 实现。请参阅此处“请注意,在 Java EE 6 应用程序服务器(例如 JBoss AS 6)中部署应用程序时,您通常不需要它。”。尝试更改项目的目标运行时。
If your local project was setup to point to Tomcat while your remote repo assumes a JavaEE server, this could be the case. See herefor the difference between Tomcat and JBoss.
如果您的本地项目设置为指向 Tomcat,而您的远程存储库假定 JavaEE 服务器,则可能是这种情况。请参阅此处了解 Tomcat 和 JBoss 之间的区别。
Edit: I changed my project to point to GlassFish instead of Tomcat and javax.persistence.*
resolved fine without any explicit JPA dependency.
编辑:我将我的项目更改为指向 GlassFish 而不是 Tomcat,并且在javax.persistence.*
没有任何显式 JPA 依赖的情况下解决得很好。
回答by Dave McLure
I ran into this same issue and realized that, since I am using spring boot, all I needed to do to resolve the issue was to add the following dependency:
我遇到了同样的问题并意识到,由于我使用的是 spring boot,我需要做的就是添加以下依赖项来解决这个问题:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
回答by Mindsect Team
If anyone is using Maven, you'll need to add the dependency in the POM.XML file. The latest version as of this post is below:
如果有人使用 Maven,则需要在 POM.XML 文件中添加依赖项。这篇文章的最新版本如下:
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
回答by 500 Server error
My solution was to select the maven profiles I had defined in my pom.xml in which I had declared the hibernate dependencies.
我的解决方案是选择我在 pom.xml 中定义的 maven 配置文件,我在其中声明了休眠依赖项。
CTRL + ALT + P
in eclipse.
CTRL + ALT + P
在日食。
In my project I was experiencing this problem and many others because in my pom I have different profiles for supporting Glassfish 3, Glassfish 4 and also WildFly so I have differet versions of Hibernate per container as well as different Java compilation targets and so on. Selecting the active maven profiles resolved my issue.
在我的项目中,我遇到了这个问题和许多其他问题,因为在我的 pom 中,我有不同的配置文件来支持 Glassfish 3、Glassfish 4 和 WildFly,所以我每个容器都有不同版本的 Hibernate 以及不同的 Java 编译目标等等。选择活动的 Maven 配置文件解决了我的问题。
回答by GMsoF
If you are using Hibernate as a JPA implementation and you are not using Maven/Gradle, the easier way is to download whole bundle instead of jar file one by one.
如果你使用 Hibernate 作为 JPA 实现并且你没有使用 Maven/Gradle,更简单的方法是下载整个包而不是一个一个的 jar 文件。
Go http://hibernate.org/orm/downloads/and download the latest library, extract the jar from the required
folder.
转到http://hibernate.org/orm/downloads/并下载最新的库,从required
文件夹中提取 jar 。
回答by Umesh_Shah
In newer hibernate jars, you can find the required jpa file under "hibernate-search-5.8.0.Final\dist\lib\provided\hibernate-jpa-2.1-api-1.0.0.Final". You have to add this jar file into your project java build path. This will most probably solve the issue.
在较新的 hibernate jar 中,您可以在“hibernate-search-5.8.0.Final\dist\lib\provided\hibernate-jpa-2.1-api-1.0.0.Final”下找到所需的 jpa 文件。您必须将此 jar 文件添加到您的项目 java 构建路径中。这很可能会解决问题。
回答by Tarun Jain
If you are using gradle with spring boot and spring JPA then add the below dependency in the build.gradle file
如果您将 gradle 与 spring boot 和 spring JPA 一起使用,请在 build.gradle 文件中添加以下依赖项
dependencies { compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.1.3.RELEASE'
依赖{编译组:'org.springframework.boot',名称:'spring-boot-starter-data-jpa',版本:'2.1.3.RELEASE'
}
}
回答by Roberto
I solved the problem by adding the following dependency
我通过添加以下依赖项解决了这个问题
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>2.2</version>
</dependency>
Together with
和...一起
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>