即使使用 noarg 插件,Kotlin 的 JPA 实体也没有默认构造函数

2024-06-26

我读到您需要kotlin-maven-noarg实体类的编译器插件,以便生成无默认参数的构造函数。

但应用程序无法启动,并出现以下错误:

实体没有默认构造函数

你能告诉我我做错了什么吗?

pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    {...}
    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>

        <testSourceDirectory>src/test/java</testSourceDirectory>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
        </testResources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <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>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>testCompile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>kotlin-maven-plugin</artifactId>
                <groupId>org.jetbrains.kotlin</groupId>
                <version>${kotlin.version}</version>

                <configuration>
                    <compilerPlugins>
                        <plugin>jpa</plugin>
                    </compilerPlugins>

                    <pluginOptions>
                        <option>jpa:annotation=javax.persistence.Entity</option>
                    </pluginOptions>
                </configuration>

                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-noarg</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

        </plugins>
    </build>

    <properties>
        {...}
        <junit.version>4.12</junit.version>
        <kotlin.version>1.1.0</kotlin.version>
    </properties>

        {...}

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
            <version>${kotlin.version}</version>
        </dependency>

    </dependencies>

</project>

测试类:

@RunWith(SpringJUnit4ClassRunner::class)
@ContextConfiguration(classes = arrayOf(VedicaConfig::class))
class InitStructures {
    private var vedicaDBInit: VedicaDBInit? = null

    @Before
    fun init() {
        vedicaDBInit = VedicaDBInit()
    }

    @Test
    fun initClientFolders() {
    }
}

我将 Intellij IDEA 与 Kotlin 1.1.0 结合使用,因此对于部署,我只需单击“调试/运行”并选择 Tomcat 运行配置,对于运行测试,我只需右键单击测试类,然后单击“运行/调试”。


你有没有尝试过运行你的 Mavencompile运行测试之前的目标?我发现 Intellij 的 Kotlin 运行/测试默认配置不会触发 Maven 插件,因此不会应用 no-arg 插件。

尝试运行你的compile目标,然后再次运行测试。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

即使使用 noarg 插件,Kotlin 的 JPA 实体也没有默认构造函数 的相关文章

随机推荐