gpt4 book ai didi

Hibernate Metamodel 注释处理器不识别 Kotlin 实体类

转载 作者:行者123 更新时间:2023-12-02 12:57:10 27 4
gpt4 key购买 nike

我有一些 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 Settings for Annotation Processing

哦。我可以在 target/generated-sources/kapt/compile/ 中看到为 Kotlin 类生成的文件而不是 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_
...

最佳答案

我删除了对 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 现在似乎也很高兴。

关于Hibernate Metamodel 注释处理器不识别 Kotlin 实体类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60530431/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com