11

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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
greengold
  • 1,184
  • 3
  • 18
  • 43
  • There is default constructor if you are using no-args plugin, but problem is that is could be accessed only through reflection, since it generated in compile time. – Stepango Apr 04 '17 at 07:11
  • I think the `no-args` only works on classes marked with a subset of annotations, you should be able to configure your own annotations for the plugin. – Gavin Jul 22 '19 at 10:45

6 Answers6

4

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.

Kongress
  • 2,244
  • 3
  • 20
  • 30
4

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

vovahost
  • 34,185
  • 17
  • 113
  • 116
3

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

LynAs
  • 6,407
  • 14
  • 48
  • 83
  • I think you could also select `classes` or `testClasses`. This is what the micronaut framework suggests you do as part of setting to develop using it. – Gavin Jul 22 '19 at 10:46
1

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!

robie2011
  • 3,678
  • 4
  • 21
  • 20
1

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

jivimberg
  • 840
  • 1
  • 10
  • 21
0

I was getting the same error until I noticed @LynAs's comment; then I remembered that there is a setting in IntelliJ that determines how to build and run your project. It is under Settings -> Build, Execution, Deployment -> Gradle -> Build and run using. Pick Gradle (Default) because if you pick IntelliJ IDEA your builds might not run fine. There is a message above that line that says:

In a pure Java/Kotlin project, building and running by means of the IDE might be faster, thanks to optimizations. Note, that the IDE doesn't support all Gradle plugins and the project might not be built correctly with some of them.

Therefore, it is preferable to use the Gradle build tool instead of IntelliJ's build tool.

enter image description here

Stefan Zhelyazkov
  • 2,599
  • 4
  • 16
  • 41