Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no default constructor for JPA entity with Kotlin even with noarg plugin

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.

like image 574
greengold Avatar asked Apr 02 '17 15:04

greengold


3 Answers

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

like image 76
vovahost Avatar answered Sep 21 '22 21:09

vovahost


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

like image 34
LynAs Avatar answered Sep 21 '22 21:09

LynAs


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!

like image 32
robie2011 Avatar answered Sep 21 '22 21:09

robie2011