You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Kotlin实体类生成Hibernate静态模型及解决编译问题

解决Spring Boot + Kotlin + Hibernate + Maven中静态模型生成的编译问题

我基于Spring Boot + Kotlin + Hibernate + Maven搭建项目,想要生成Hibernate静态模型,但编译时出现以下报错:

cannot find symbol

symbol: class SomeEntity

location: class com.default.SomeEntity_

报错出现在自动生成的SomeEntity_类中,具体是@StaticMetamodel(SomeEntity.class)这一行代码。

我当前的Kotlin Maven插件配置如下:

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <configuration>
        <compilerPlugins>
            <plugin>spring</plugin>
            <plugin>all-open</plugin>
            <plugin>jpa</plugin>
        </compilerPlugins>
        <pluginOptions>
            <option>all-open:annotation=javax.persistence.Entity</option>
            <option>all-open:annotation=javax.persistence.Embeddable</option>
            <option>all-open:annotation=javax.persistence.MappedSuperclass</option>
        </pluginOptions>
    </configuration>
    <executions>
        <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/kotlin</sourceDir>
                    <sourceDir>src/main/java</sourceDir>
                </sourceDirs>
                <annotationProcessorPaths>
                    <annotationProcessorPath>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                        <version>${hibernate.version}</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-noarg</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>
</plugin>

编辑补充:给kapt添加<phase>process-classes</phase>后,模型成功生成,但使用这些模型的代码无法编译——因为代码试图访问的是编译完成后才生成的类。修改后的kapt执行配置如下:

<execution>
    <id>kapt</id>
    <phase>process-classes</phase>
    <goals>
        <goal>kapt</goal>
    </goals>
    <configuration>
        <annotationProcessorPaths>
            <annotationProcessorPath>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-jpamodelgen</artifactId>
                <version>${hibernate.version}</version>
            </annotationProcessorPath>
        </annotationProcessorPaths>
    </configuration>
</execution>

解决方案

1. 核心问题分析

问题出在注解处理器执行时机与代码编译顺序不匹配:

  • 原配置未指定kapt阶段,默认执行时机晚于Kotlin编译,导致生成模型类时找不到原实体类
  • 改成process-classes阶段则会让模型生成在主代码编译之后,导致使用模型的代码无法引用到这些类

正确的做法是让kapt在Kotlin编译前执行,同时确保生成的模型类能被后续编译识别。

2. 修正后的完整Kotlin Maven插件配置

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <configuration>
        <compilerPlugins>
            <plugin>spring</plugin>
            <plugin>all-open</plugin>
            <plugin>jpa</plugin>
        </compilerPlugins>
        <pluginOptions>
            <option>all-open:annotation=javax.persistence.Entity</option>
            <option>all-open:annotation=javax.persistence.Embeddable</option>
            <option>all-open:annotation=javax.persistence.MappedSuperclass</option>
        </pluginOptions>
    </configuration>
    <executions>
        <!-- 新增Kotlin主代码编译执行阶段 -->
        <execution>
            <id>compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <!-- Kotlin测试代码编译 -->
        <execution>
            <id>test-compile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
        </execution>
        <!-- kapt注解处理 -->
        <execution>
            <id>kapt</id>
            <phase>process-sources</phase>
            <goals>
                <goal>kapt</goal>
            </goals>
            <configuration>
                <sourceDirs>
                    <sourceDir>src/main/kotlin</sourceDir>
                    <sourceDir>src/main/java</sourceDir>
                </sourceDirs>
                <annotationProcessorPaths>
                    <annotationProcessorPath>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                        <version>${hibernate.version}</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
                <!-- 指定生成类目录,确保后续编译能识别 -->
                <generatedSourcesDirectory>${project.build.directory}/generated-sources/kapt</generatedSourcesDirectory>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-noarg</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>
</plugin>

关键调整说明

  • 新增compile执行阶段,明确Kotlin主代码编译在compile阶段运行
  • 将kapt的执行阶段设为process-sources,确保在Kotlin编译前完成静态模型生成
  • 保留sourceDirs配置,让注解处理器能扫描到Kotlin/Java实体类
  • 指定generatedSourcesDirectory,确保生成的模型类被Maven识别为源码目录,后续编译能正常引用

调整后编译顺序:

  1. kapt处理注解生成静态模型类
  2. Kotlin编译主代码(包括引用模型类的代码)
  3. 测试代码编译

既能成功生成模型类,又能让使用模型的代码正常编译。

内容的提问来源于stack exchange,提问作者Damian Kapłon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 22:24:54