我应该包含什么 jar 才能在基于休眠的应用程序中使用 javax.persistence 包?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/737496/
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
What jar should I include to use javax.persistence package in a hibernate based application?
提问by Sergey
Is it ok to take it from Glassfish project ( glassfish-persistence-api) or may be there is a Hibernate jar?
可以从 Glassfish 项目( glassfish-persistence-api )中获取它还是可能有一个 Hibernate jar?
采纳答案by Schildmeijer
If you are developing an OSGi system I would recommend you to download the "bundlefied" version from Springsource Enterprise Bundle Repository.
如果您正在开发 OSGi 系统,我建议您从Springsource Enterprise Bundle Repository下载“bundlefied”版本。
Otherwise its ok to use a regular jar-file containing the javax.persistencepackage
否则可以使用包含javax.persistence包的常规 jar 文件
回答by Jerrish Varghese
hibernate.jar and hibernate-entitymanager.jar contains only the packages org.hibernate.*. So you should take it from the Glassfish project.
hibernate.jar 和 hibernate-entitymanager.jar 只包含包 org.hibernate.*。所以你应该从 Glassfish 项目中获取它。
回答by alves
You can use the ejb3-persistence.jarthat's bundled with hibernate. This jar only includes the javax.persistence package.
您可以使用与 hibernate 捆绑在一起的ejb3-persistence.jar。这个 jar 只包含 javax.persistence 包。
回答by Dave Shah
In the latest and greatest Hibernate, I was able to resolve the dependency by including the hibernate-jpa-2.0-api-1.0.0.Final.jar within lib/jpa directory. I didn't find the ejb-persistence jar in the most recent download.
在最新最好的 Hibernate 中,我能够通过在 lib/jpa 目录中包含 hibernate-jpa-2.0-api-1.0.0.Final.jar 来解决依赖关系。我在最近的下载中没有找到 ejb-persistence jar。
回答by Bahad?r Ya?an
If you are using maven, adding below dependency should work
如果您使用的是 maven,则添加以下依赖项应该可以工作
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
回答by antonycc
For JPA 2.1 the javax.persistence package can be found in here:
对于 JPA 2.1,可以在此处找到 javax.persistence 包:
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
See: hibernate-jpa-2.1-api on Maven CentralThe pattern seems to be to change the artefact name as the JPA version changes. If this continues new versions can be expected to arrive in Maven Central here: Hibernate JPA versions
请参阅:Maven Central 上的 hibernate-jpa-2.1-api该模式似乎是随着 JPA 版本的变化而更改人工制品名称。如果这种情况继续下去,预计新版本将到达 Maven 中心:Hibernate JPA 版本
The above JPA 2.1 APi can be used in conjunction with Hibernate 4.3.7, specifically:
上面的JPA 2.1 API可以和Hibernate 4.3.7结合使用,具体来说:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.7.Final</version>
</dependency>
回答by yaromir
In general, i agree with above answers that recommend to add maven dependency, but i prefer following solution.
一般来说,我同意上述建议添加 maven 依赖项的答案,但我更喜欢以下解决方案。
Add a dependency with API classes for full JavaEE profile:
为完整的 JavaEE 配置文件添加与 API 类的依赖关系:
<properties>
<javaee-api.version>7.0</javaee-api.version>
<hibernate-entitymanager.version>5.1.3.Final</hibernate-entitymanager.version>
</properties>
<depencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee-api.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
Also add dependency with particular JPA provider like antonycc suggested:
还要添加对特定 JPA 提供程序的依赖,如 antonycc 建议:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate-entitymanager.version}</version>
</dependency>
Note <scope>provided</scope>
in API dependency section: this means that corresponding jar will not be exported into artifact's lib/
, but will be provided by application server. Make sure your application server implements specified version of JavaEE API.
<scope>provided</scope>
API依赖部分注意:这意味着相应的jar不会导出到artifact的 中lib/
,而是由应用服务器提供。确保您的应用服务器实现了指定版本的 JavaEE API。