hibernate - 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 中启用注释处理:
哦。我可以看到为 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_
...
解决方案
我删除了对 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 现在似乎也很高兴。
推荐阅读
- tensorflow - 在 Ubuntu 18.04 中安装 conda tensorflow-gpu 时是否应将显卡物理安装在 PV 中
- php - Laravel 会话数据不老化
- react-native - React Native:console.error:“没有为密钥“auth”提供减速器”
- jquery - Laravel 中的 jQuery/AJAX 多文件上传
- sql - 从宏查询(加入)外部工作簿
- jquery - 我可以将 Ajax 与 asp.net 4.5 .NET Framework FileUpload 控件一起使用来上传多个文件吗?
- javascript - 仅从 firebase 实时数据库中获取当前用户数据
- dynamics-crm - 计算与该特定公司相关的每个人的其他关系
- testing - 跨多个内核测试内核模块
- excel - 如果超过 1 个选项,则自动填充/带有数据验证的下拉列表