no default constructor for JPA entity with Kotlin

2020-08-17 12:36发布

I have read that you need the kotlin-maven-noarg compiler plugin for entity classes in order for it to generate default-parameter less cosntructor.

But the application does not start with the following error:

No default constructor for entity

Can you tell me what am I doing wrong?

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>

test class:

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

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

    @Test
    fun initClientFolders() {
    }
}

I'm using Intellij IDEA with Kotlin 1.1.0, so for deployment I just click debug/run with Tomcat run configuration selected and for running test I just right click on the test class and also click run/debug.

5条回答
别忘想泡老子
2楼-- · 2020-08-17 12:47

Have you tried running your Maven compile goal before running your test? I've found that Intellij's default configurations for Kotlin run/test don't fire Maven plugins and therefore don't apply the no-arg plugin.

Try running your compile goal and then running your test again.

查看更多
老娘就宠你
3楼-- · 2020-08-17 12:48

Finally after a few attempts found how to fix it:

  • Open Run/Debug Configurations window (Run > Edit Configurations)
  • Select your existing Spring boot run configuration
  • Add the following presets in Program arguments field:

    -Xplugin=$KOTLIN_HOME/lib/noarg-compiler-plugin.jar -P plugin:org.jetbrains.kotlin.noarg:preset=jpa

  • Click ok to save configuration
  • Stop and Run project again

Run configuration screenshot

Useful link: No-arg compiler plugin info

查看更多
干净又极端
4楼-- · 2020-08-17 12:56

I had similar issue with intellij/gradle/kotlin.

In my case I always started the webapplication by calling the main method through intellij editor Run-Icon. I don't know what excatly is going wrong but somehow the build process wasn't finished correctly.

In contrast, If I started the webapplication from console by calling ./gradlew bootRun everything worked fine!

查看更多
闹够了就滚
5楼-- · 2020-08-17 13:02

I had the same problem. I was using gradle build btw.

What worked for me was, Instead of running default run config that intellij provides I created a new run config of type gradle

On the Gradle project selected my project and on Tasks selected bootRun

And then ran my project and it worked for me

enter image description here

查看更多
兄弟一词,经得起流年.
6楼-- · 2020-08-17 13:07

This was an Intellij bug and has been fixed in the following versions:

  • IDEA 171.2455 + Kotlin plugin 1.1.0-dev-6111.
  • IDEA 171.2272.14 (EAP) + Kotlin plugin 1.1.0-beta-17.

Details here: https://youtrack.jetbrains.com/issue/KT-15686

查看更多
登录 后发表回答