首页 > 解决方案 > Hibernate Metamodel 注释处理器无法识别 Kotlin 实体类

问题描述

我在 Java 中有一些实体类,在 Kotlin 中有一些实体类。我刚刚在我的 pom.xml 中添加了以下内容:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

IntelliJ 现在显示java实体的元模型类,但不显示kotlin实体。所以我添加了 kapt 无效:

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <version>${kotlin.version}</version>
    <executions>
        <execution>
            <id>compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <execution>
            <id>test-compile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
        </execution>
        <execution>
            <id>kapt</id>
            <goals>
                <goal>kapt</goal>
            </goals>
            <configuration>
                <sourceDirs>
                    <sourceDir>src/main/java</sourceDir>
                </sourceDirs>
                <annotationProcessorPaths>
                    <!-- Specify your annotation processors here. -->
                    <annotationProcessorPath>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                        <version>${hibernate.version}</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <jvmTarget>11</jvmTarget>
        <args>
            <arg>-Xjvm-default=enable</arg>
            <!--<arg>-XXLanguage:+JvmStaticInInterface</arg>-->
        </args>
        <compilerPlugins>
            <plugin>jpa</plugin>
<!--                        <plugin>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</plugin>-->
        </compilerPlugins>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-noarg</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
    </dependencies>
</plugin>

我尝试添加<plugin>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</plugin>没有明显效果。我尝试在 IntelliJ IDEA 中启用注释处理: 注释处理的 IntelliJ 设置

哦。我可以看到为 Kotlin 类生成的文件,target/generated-sources/kapt/compile/而不是target/generated-sources/annotations/. 这可能就像设置适当的输出目录一样简单。

现在,当我运行时,mvn clean compile我只收到 Java 实体类的错误,说注释已经运行(可能是由 kapt 运行)并创建了类:

[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MyProject ---
[INFO] Deleting /myproject/target
[INFO] 
[INFO] --- kotlin-maven-plugin:1.3.70:kapt (kapt) @ MyProject ---
[WARNING] 'tools.jar' was not found, kapt may work unreliably
[INFO] Applied plugin: 'jpa'
[INFO] Note: Hibernate JPA 2 Static-Metamodel Generator 5.4.12.Final
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MyProject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 200 resources
[INFO] 
[INFO] --- kotlin-maven-plugin:1.3.70:compile (compile) @ MyProject ---
[INFO] Applied plugin: 'jpa'
[WARNING] Duplicate source root: /myproject/target/generated-sources/kapt/compile
[WARNING] Duplicate source root: /myproject/target/generated-sources/kaptKotlin/compile
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (java-compile) @ MyProject ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 261 source files to /myproject/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Problem with Filer: Attempt to recreate a file for type my.project.db.SomeJavaClass_
[ERROR] Problem with Filer: Attempt to recreate a file for type my.project.db.AnotherJavaClass_
...

标签: hibernatemavenkotlinannotation-processingkapt

解决方案


我删除了对 hibernate-jpamodelgen 的项目依赖和对 Kotlin 插件的依赖,只在 kapt 的 annotationProcessorPath 中提到了它。Maven 成功完成。

<!-- Java has a fit if Kapt processes this first,
     so just let Kapt do it and don't tell Java about it. -->
<!--        <dependency>-->
<!--            <groupId>org.hibernate</groupId>-->
<!--            <artifactId>hibernate-jpamodelgen</artifactId>-->
<!--            <version>${hibernate.version}</version>-->
<!--        </dependency>-->

我还回到了 IntelliJ 设置窗口,删除了 JPAMetaModelEntityProcessor 并取消选中“注释处理”。IntelliJ 现在似乎也很高兴。


推荐阅读