Eclipse 警告:找不到类 javax.persistence.*
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24812730/
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
Eclipse Warnings: class javax.persistence.* not found
提问by Travis Parks
This is my first time really playing around with Java development using Eclipse. I am trying to use EclipseLink's implementation of the JPA. I moved all of my entities into a separate package "entities". I have the persistence.xml
in a separate JPA project called "dataModeling".
这是我第一次真正尝试使用 Eclipse 进行 Java 开发。我正在尝试使用 EclipseLink 的 JPA 实现。我将所有实体移动到一个单独的包“实体”中。我有persistence.xml
一个名为“dataModeling”的单独 JPA 项目。
Everything builds and runs fine.
一切都建立并运行良好。
Just about every project depends on my entities. However, I'm seeing a warning Class javax.persistence.Entity not found - continuing with a stub.
, etc. showing up because the dependent projects don't reference EclipseLink.
几乎每个项目都取决于我的实体。但是,我看到警告Class javax.persistence.Entity not found - continuing with a stub.
等出现,因为依赖项目没有引用 EclipseLink。
The solution is to go into each dependent project's properties and under Java Build Path > Libraries, click Add Library, then User Libraryand then select EclipseLink.
解决方案是进入每个依赖项目的属性,在Java Build Path > Libraries 下,单击Add Library,然后单击User Library,然后选择EclipseLink。
However, to me, it doesn't make sense to reference EclipseLink in every project! That's an implementation detail I don't want to burden other projects with. It looks like this is happening because the other projects see the annotations and don't recognize them.
但是,对我来说,在每个项目中都引用 EclipseLink 没有意义!这是我不想给其他项目带来负担的实现细节。看起来这是因为其他项目看到了注释并且无法识别它们。
So the real question is: how can I use JPA (via annotations) without every other project needing to reference my JPA implementation?
所以真正的问题是:如何使用 JPA(通过注释)而不需要每个其他项目都需要引用我的 JPA 实现?
采纳答案by Travis Parks
Thanks to @neil-stockton and @chris, I was able to figure out what was going on. Most JPA implementations have a copy of the javax.persistence JAR floating around somewhere. Most of them are bundled with everything else, leading to my dependency nightmare. There doesn't appear to be a de facto implementation floating around.
感谢@neil-stockton 和@chris,我能够弄清楚发生了什么。大多数 JPA 实现都有一个 javax.persistence JAR 的副本在某处浮动。它们中的大多数都与其他所有东西捆绑在一起,导致我的依赖噩梦。似乎没有一个事实上的实现。
In my case, I used the copy that showed up under my Eclipse plugins directory. These annotations were truly empty in that did not have any unwanted dependencies. The JAR file (javax.persistence._<version>.jar
) only showed up after I added the Dali and EclipseLink plugins (one or the other).
就我而言,我使用了 Eclipse 插件目录下显示的副本。这些注释确实是空的,因为没有任何不需要的依赖项。JAR 文件 ( javax.persistence._<version>.jar
) 仅在我添加 Dali 和 EclipseLink 插件(一个或另一个)后出现。
回答by Maciej Dobrowolski
Your pom.xml
should contain:
您pom.xml
应该包含:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.0</version>
<scope>compile</scope>
</dependency>
the first one is Eclipse-Link
(which you already have), the second one is Persistence API
which is lacking.
第一个是Eclipse-Link
(你已经有了),第二个Persistence API
是缺少的。
If you are not using maven - make sure that javax.persistence-2.0.0.jar
is on your classpath.
如果您不使用 maven - 确保它javax.persistence-2.0.0.jar
在您的类路径中。
Note that this is version 2.0.0, the newest is 2.1.0
注意这是2.0.0版本,最新的是2.1.0
updateThe project which makes use of EntityManager
should have these dependences. Putting entities and persistence.xml
in separate jar file still requires the other project that uses it to fulfill above dependencies.
update使用的项目EntityManager
应该有这些依赖。将实体和persistence.xml
单独的 jar 文件放在单独的 jar 文件中仍然需要使用它来实现上述依赖项的另一个项目。