Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Getters of lazy classes cannot be final Kotlin Spring Boot

I try to create REST Spring Boot Server on Kotlin.I used database - first and automatically generated entities using the built-in Intellij tool. When starting the server pop-up error:

2020-01-12 15:09:14.387 ERROR 23016 --- [  restartedMain] o.h.tuple.entity.PojoEntityTuplizer      : HHH000112: Getters of lazy classes cannot be final: ru.madbrains.smartfridgemanager.server.model.MealplanEntity.name
2020-01-12 15:09:14.391 ERROR 23016 --- [  restartedMain] o.h.tuple.entity.PojoEntityTuplizer      : HHH000243: Setters of lazy classes cannot be final: ru.madbrains.smartfridgemanager.server.model.MealplanEntity.name
2020-01-12 15:09:14.391 ERROR 23016 --- [  restartedMain] o.h.tuple.entity.PojoEntityTuplizer      : HHH000112: Getters of lazy classes cannot be final: ru.madbrains.smartfridgemanager.server.model.MealplanEntity.refRecipeEntities
2020-01-12 15:09:14.391 ERROR 23016 --- [  restartedMain] o.h.tuple.entity.PojoEntityTuplizer      : HHH000243: Setters of lazy classes cannot be final: ru.madbrains.smartfridgemanager.server.model.MealplanEntity.refRecipeEntities

My Entity:

import javax.persistence.*

@Entity
@Table(name = "mealplan", schema = "public", catalog = "smartfridgemanagerdatabase")
open class MealplanEntity {
    @get:Id
    @get:Column(name = "id", nullable = false, insertable = false, updatable = false)
    var id: Int? = null
    @get:Basic
    @get:Column(name = "name", nullable = true)
    var name: Int? = null

    @get:OneToMany(mappedBy = "refMealplanEntity")
    var refRecipeEntities: List<RecipeEntity>? = null

    override fun toString(): String =
            "Entity of type: ${javaClass.name} ( " +
                    "id = $id " +
                    "name = $name " +
                    ")"

    // constant value returned to avoid entity inequality to itself before and after it's update/merge
    override fun hashCode(): Int = 42

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false
        other as MealplanEntity

        if (id != other.id) return false
        if (name != other.name) return false

        return true
    }
}

What's the matter?

Edit 1: My build.gradle.kts file: (was generated using Spring Initializr - start.spring.io)

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
    id("org.springframework.boot") version "2.2.2.RELEASE"
    id("io.spring.dependency-management") version "1.0.8.RELEASE"
    kotlin("jvm") version "1.3.61"
    kotlin("plugin.spring") version "1.3.61"
    kotlin("plugin.jpa") version "1.3.61"
}

group = "ru.madbrains.smartfridgemanager"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

val developmentOnly by configurations.creating
configurations {
    runtimeClasspath {
        extendsFrom(developmentOnly)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.0.0.RELEASE")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.postgresql:postgresql")
    implementation("io.jsonwebtoken:jjwt:0.9.1")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}
like image 638
apelsinkach Avatar asked Jan 12 '20 13:01

apelsinkach


2 Answers

Adding the open attribute to the properties solved the problem for me. E. g.

open var name: Int? = null
like image 173
Tom Reineke Avatar answered Oct 28 '22 23:10

Tom Reineke


Expanding on JB Nizets comment to configure the all-open plugin.

If you use the all-open plugin, you can make it include your Entity-Classes by adding a pluginOptions section:

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <configuration>
        <compilerPlugins>
            <plugin>all-open</plugin>
        </compilerPlugins>

        <pluginOptions>
            <option>all-open:annotation=javax.persistence.Entity</option>
        </pluginOptions>
    </configuration>
</plugin>

This will make the all-open plugin process all your classes annotated with the JPA @Entity annotation so that they (and also their functions) will become non-final.

like image 22
Markus Rohlof Avatar answered Oct 28 '22 23:10

Markus Rohlof