Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which module should I require in Java 9 to use JPA?

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.

[email protected] 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

like image 534
Giovanni Lovato Avatar asked Sep 28 '17 14:09

Giovanni Lovato


People also ask

What are modules in Java 9?

Defining the Java 9 module. A module is a collection of code, data, and resources. It is a set of related packages and types (classes, abstract classes, interfaces, and more) with code, data files, and some static resources. For example, the module descriptor module-info.

Is JPA part of Java EE?

The Java Persistence API was first released as a subset of the Enterprise JavaBeans 3.0 specification (JSR 220) in Java EE 5. It has since evolved as its own spec, starting with the release of JPA 2.0 in Java EE 6 (JSR 317). JPA was adopted as an independent project of Jakarta EE in 2019.

Which is correct about module system in Java 9?

Java Module System is a major change in Java 9 version. Java added this feature to collect Java packages and code into a single unit called module. In earlier versions of Java, there was no concept of module to create modular Java applications, that why size of application increased and difficult to move around.


2 Answers

With maven, 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 gradle

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

like image 108
Naman Avatar answered Oct 03 '22 17:10

Naman


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.

like image 41
Alan Bateman Avatar answered Oct 03 '22 19:10

Alan Bateman