9

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.

I searched a lot, but I couldn't find the module to require to use JPA in a Java 9 module project.

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

But still with this module-info.java

module my.module {

    requires java.persistence;

}

I get "module-info.java:[3,18] module not found: java.persistence".

UPDATE 2 This is my project structure:

.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   ├── my
    │   │   │   └── module
    │   │   │       └── MyBean.java
    │   │   └── module-info.java
    │   └── resources
    └── test
        ├── java
        └── resources

The pom.xml has the javax.persistence-api dependency.

Testing repo: https://github.com/heruan/java9-jpa

Giovanni Lovato
  • 2,183
  • 2
  • 29
  • 53
  • `requires` should use the file name for automatic modules: `javax.persistence-api-2.2` but that looks weird. I would rename the jar? – Joop Eggen Sep 28 '17 at 15:12
  • 2
    @JoopEggen The automatic module naming if I am not wrong, converts any non-alphanumeric character into a dot(`.`). So the current module name would end up being `javax.persistence.api` in that case(ofcourse without version number which are not maintained at module level) – Naman Sep 28 '17 at 15:33

3 Answers3

7

With , you can use a dependency like

<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>

with maven-compiler-plugin as updated to work with jdk9 detailed here.

Similar with dependency with

compile group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'

which is based out of the javaee/jpa-spec. This would facilitate the

requires java.persistence 

as an automatic module as proposed to be the name intended for the module here.


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
...

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'. ;)

Naman
  • 27,789
  • 26
  • 218
  • 353
  • Thank you, but I already have that dependency in my `pom.xml`. – Giovanni Lovato Sep 28 '17 at 15:04
  • @GiovanniLovato the name of the module you tried is incorrect though – Naman Sep 28 '17 at 15:05
  • Corrected, but still "module not found: java.persistence". – Giovanni Lovato Sep 28 '17 at 15:13
  • Not sure of what your project structure looks like. Just make sure that the jar is available on the module path in that case. – Naman Sep 28 '17 at 15:15
  • Updated question with project structure! Don't know if Maven dependencies are included in module path. – Giovanni Lovato Sep 28 '17 at 15:23
  • The structure looks fine to me. Can you share the relevant pom.xml as well and does the project build with maven(if you tried)? – Naman Sep 28 '17 at 15:32
  • Repo at https://github.com/heruan/java9-jpa (Maven fails with "module not found: java.persistence"). – Giovanni Lovato Sep 28 '17 at 15:44
  • @GiovanniLovato Fails for me for one reason.. takes the deault `maven-compiler-plugin 3.1` which doesn't support jdk9. So try modifying the plugin as [mentioned here](https://maven.apache.org/plugins/maven-compiler-plugin/examples/module-info.html) – Naman Sep 28 '17 at 15:50
  • 2
    Forcing Maven compiler to 3.7.0 solved the issue compiling in Maven, not in Eclipse though—but that's an Eclipse issue. Thank you! – Giovanni Lovato Sep 28 '17 at 15:55
2

I don't think that JPA has been published as a module yet but you should be able to use it as an automatic module. Can you use jar --file=<jarfile> --describe-module to see what module name is derived for the JPA JAR file.

Alan Bateman
  • 5,283
  • 1
  • 20
  • 25
0

Try to use this maven dependency:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.5.2.Final</version>
</dependency>