我应该在Java 9中使用哪个模块来使用JPA? [英] Which module should I require in Java 9 to use JPA?
问题描述
我正在使用需要JPA( javax.persistence。*
类)的项目测试Java 9。当我添加 module-info.java
并声明我的模块时, javax.persistece
包下的所有类都变得不可用。
I'm testing Java 9 with a project which requires JPA (javax.persistence.*
classes). When I add the module-info.java
and declare my module, all classes under javax.persistece
package become unavailable.
我搜索了很多,但我找不到要求在Java 9模块项目中使用JPA的模块。
I searched a lot, but I couldn't find the module to require to use JPA in a Java 9 module project.
更新
正如Alan所说,我跑了
UPDATE As Alan suggested, I ran
$ jar --describe-module --file=javax.persistence-api-2.2.jar
No module descriptor found. Derived automatic module.
java.persistence@2.2 automatic
requires java.base mandated
contains javax.persistence
contains javax.persistence.criteria
contains javax.persistence.metamodel
contains javax.persistence.spi
但仍然使用此 module-info.java
module my.module {
requires java.persistence;
}
我得到module-info.java:[3,找不到模块:java.persistence。
I get "module-info.java:[3,18] module not found: java.persistence".
UPDATE 2
这是我的项目结构:
UPDATE 2 This is my project structure:
.
├── pom.xml
└── src
├── main
│ ├── java
│ │ ├── my
│ │ │ └── module
│ │ │ └── MyBean.java
│ │ └── module-info.java
│ └── resources
└── test
├── java
└── resources
pom.xml
具有 javax.persistence-api
依赖项。
测试repo: https://github.com/heruan/java9-jpa
推荐答案
使用 maven ,您可以使用依赖项,例如
With maven, you can use a dependency like
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
maven-compiler-plugin
已更新使用jdk9 详细说明。
with maven-compiler-plugin
as updated to work with jdk9 detailed here.
类似于依赖 gradle
compile group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
基于 javaee / jpa-spec 。这将促进
requires java.persistence
作为自动模块,建议用作模块的名称这里。
as an automatic module as proposed to be the name intended for the module here.
添加细节,这是在 <中定义的em> META-INF / MANIFEST.MF as:
Adding to the details, this is defined in the META-INF/MANIFEST.MF as :
Manifest-Version: 1.0
Bundle-Description: Java(TM) Persistence 2.2 API jar
Automatic-Module-Name: java.persistence
...
注意 - 根据Alan的建议找出模块名称的方法很准确,但没有广告,我仍然我喜欢使用一些类的包,然后当我说'import class'然后'添加要求'时,让IntelliJ(2017.2.4)为我做那个解决方案。 ;)
Note- The way to figure out the module name as suggested by Alan is precise, but no advertising and I still prefer using a class of some package and then let IntelliJ(2017.2.4) do that resolution for me when I say 'import class' and then 'add requires'. ;)
这篇关于我应该在Java 9中使用哪个模块来使用JPA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!