Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate-jpamodelgen 6.5.2.Final throws NullPointerException at Java module

After upgrading org.hibernate.orm:hibernate-jpamodelgen from 6.3.1.Final to 6.5.2.Final (by migrating to Spring Boot 3.3.0) I get the following NullPointerException during annotation processing.

[ERROR] Error running Hibernate processor: Cannot invoke "javax.lang.model.element.Element.getAnnotationMirrors()" because "element" is null
[ERROR] java.lang.NullPointerException: Cannot invoke "javax.lang.model.element.Element.getAnnotationMirrors()" because "element" is null
    at org.hibernate.processor.util.TypeUtils.getAnnotationMirror(TypeUtils.java:251)
    at org.hibernate.processor.util.TypeUtils.hasAnnotation(TypeUtils.java:260)
    at org.hibernate.processor.HibernateProcessor.processClasses(HibernateProcessor.java:297)
    at org.hibernate.processor.HibernateProcessor.process(HibernateProcessor.java:261)
    at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:1023)
    at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:939)
    at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1267)
    at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1382)
    at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1234)
    at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:916)
    at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:104)
    at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.invocationHelper(JavacTaskImpl.java:152)
    at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:100)
    at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:94)
    at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess(JavaxToolsCompiler.java:126)
    at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile(JavacCompiler.java:214)
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:1228)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:215)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:972)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:196)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)

Debugging shows that the problematic element is a Java module (an instance of com.sun.tools.javac.code.Symbol.ModuleSymbol). So the call context.getElementUtils().getPackageOf(element) (org.hibernate.processor.HibernateProcessor.processClasses(RoundEnvironment roundEnvironment):297) gives null. The code of com.sun.tools.javac.model.JavacElements is straightforward.

    @DefinedBy(Api.LANGUAGE_MODEL)
    public PackageElement getPackageOf(Element e) {
        if (e.getKind() == ElementKind.MODULE)
            return null;
        else
            return cast(Symbol.class, e).packge();
    }

I use Apache Maven 3.8.4 and maven-compiler-plugin 3.13.0. My JDK is the following.

$ java --version
openjdk 17 2021-09-14
OpenJDK Runtime Environment Temurin-17+35 (build 17+35)
OpenJDK 64-Bit Server VM Temurin-17+35 (build 17+35, mixed mode, sharing)

Is it a bug in hibernate-jpamodelgen or JDK? Is there any setting to avoid such a problem?

like image 773
Yury Shafran Avatar asked Jan 23 '26 13:01

Yury Shafran


2 Answers

I have a similar error with 6.5.2.Final version of hibernate-modelgen, version 6.4.8.Final is the latest I found to run without this problem.

like image 110
r-uu Avatar answered Jan 25 '26 01:01

r-uu


Upgrade both hiberante and hibernate-jpamodel gen to 6.6.2.Final or 6.6.3.Final

like image 24
walkeros Avatar answered Jan 25 '26 02:01

walkeros



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!