是否可以为Java 11和java模块生成hibernate元模型?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了是否可以为Java 11和java模块生成hibernate元模型?相关的知识,希望对你有一定的参考价值。

我有一个带有JPA实体和persistence.xml的java模块,我想生成元模型。我尝试了两种变体。

变体1

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.target}</target>
                <annotationProcessorPaths>
                        <annotationProcessorPath>
                                <groupId>org.hibernate</groupId>
                                <artifactId>hibernate-jpamodelgen</artifactId>
                                <version>5.4.14.Final</version>
                        </annotationProcessorPath>
                        <path>
                                <groupId>javax.xml.bind</groupId>
                                <artifactId>jaxb-api</artifactId>
                                <version>2.3.0</version>
                        </path>
                        <path>
                                <groupId>javax.annotation</groupId>
                                <artifactId>javax.annotation-api</artifactId>
                                <version>1.3.1</version>
                        </path>
                </annotationProcessorPaths>
            </configuration>
            <dependencies>
            </dependencies>
        </plugin>

结果:

Caused by: java.lang.NullPointerException
    at org.hibernate.jpamodelgen.ClassWriter.writeGeneratedAnnotation(ClassWriter.java:202)
    at org.hibernate.jpamodelgen.ClassWriter.generateBody(ClassWriter.java:100)
    at org.hibernate.jpamodelgen.ClassWriter.writeFile(ClassWriter.java:53)
    at org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor.createMetaModelClasses(JPAMetaModelEntityProcessor.java:145)
    at org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor.process(JPAMetaModelEntityProcessor.java:135)
    at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:972)
    at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:888)
    at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1214)
    at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1326)
    at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1258)
    at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:936)
    at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:104)
    at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.handleExceptions(JavacTaskImpl.java:147)
    ... 28 more

变式2

    <plugin>
        <groupId>org.bsc.maven</groupId>
        <artifactId>maven-processor-plugin</artifactId>
        <version>3.3.3</version>
        <executions>
            <execution>
                <goals>
                    <goal>process</goal>
                </goals>
                <phase>generate-sources</phase>
                <configuration>
                    <compilerArguments>-AaddGeneratedAnnotation=false</compilerArguments>
                    <processors>
                      <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                    </processors>
                </configuration>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-jpamodelgen</artifactId>
                <version>5.4.14.Final</version>
            </dependency>
        </dependencies>
    </plugin>

结果出现了很多类似下面的错误。

[ERROR] diagnostic: /home/<deleted>/src/main/java/module-info.java:11: error: module not found: org.slf4j
    requires org.slf4j;
                ^
[ERROR] diagnostic: /home/<deleted>/src/main/java/module-info.java:15: error: module not found: java.persistence
    requires java.persistence;
                 ^

是否可以为Java 11和java模块生成hibernate模型?

答案

以前不可能,现在可以了。我为 maven-processor-plugin 他们做了必要的修改。所以,使用版本 4.0-rc1+ 和以下代码:

        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>4.0-rc1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <processors>
                            <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                        </processors>
                        <compilerArguments>-AaddGeneratedAnnotation=false</compilerArguments>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-jpamodelgen</artifactId>
                    <version>5.4.14.Final</version>
                </dependency>
            </dependencies>
        </plugin>

以上是关于是否可以为Java 11和java模块生成hibernate元模型?的主要内容,如果未能解决你的问题,请参考以下文章

是否可以发布带有环境模块声明的包?

如何用java 将文件加密压缩为zip文件.

为续集自动生成模型

11-开发板接入小五物联实现远程控制(Wi-Fi模块,远程升级自己的程序)

WIFI模块在网络中的作用?

是否可以使用 EclipseLink 输出生成的 SQL 而无需增加日志的详细程度?