0

相关信息:我正在使用 Kotlin 和 Hibernate 和 gradle。

也许相关信息:我根本没有使用 Spring。我正在使用 Micronaut 数据

问题:

当我尝试一个简单的 get

curl --location --request GET 'localhost:8080/accountholders?id=1'

我明白了

[default-nioEventLoopGroup-1-3] ERROR i.m.h.s.netty.RoutingInBoundHandler - Unexpected error occurred: org.hibernate.InstantiationException: No default constructor for entity:  : com.mybank.model.AccountHolder
javax.persistence.PersistenceException: org.hibernate.InstantiationException: No default constructor for entity:  : com.mybank.model.AccountHolder
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1542)
    at org.hibernate.query.internal.AbstractProducedQuery.uniqueResult(AbstractProducedQuery.java:1575)
    at org.hibernate.query.internal.AbstractProducedQuery.uniqueResultOptional(AbstractProducedQuery.java:1526)
    at io.micronaut.data.hibernate.operations.HibernateJpaOperations.lambda$findOne$2(HibernateJpaOperations.java:173)
    at io.micronaut.transaction.support.AbstractSynchronousTransactionManager.executeRead(AbstractSynchronousTransactionManager.java:155)
...
Caused by: org.hibernate.InstantiationException: No default constructor for entity:  : com.mybank.model.AccountHolder
    at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:85)

环顾四周,我从kotlin 参考资料中阅读了关于 no-arg-compiler-plugin 的信息。所以我添加了,但我根本没有看到任何变化。可能有一个额外的步骤,例如

noArg {
    annotation("com.my.Annotation")
}

noArg {
    invokeInitializers = true
}

拜托,有人可以看看我的 build.gradle 和实体告诉我到目前为止我缺少什么吗?

构建.gradle

plugins {
    id "org.jetbrains.kotlin.jvm" version "1.4.10"
    id "org.jetbrains.kotlin.kapt" version "1.4.10"
    id "org.jetbrains.kotlin.plugin.allopen" version "1.4.10"
    id "com.github.johnrengelman.shadow" version "6.1.0"
    id "io.micronaut.application" version '1.0.5'
    id "org.jetbrains.kotlin.plugin.noarg" version "1.4.10"
}

noArg {
    annotation("com.my.Annotation")
}

version "0.1"
group "com.mybank"

repositories {
    mavenCentral()
    jcenter()
}

micronaut {
    runtime "netty"
    testRuntime "junit5"
    processing {
        incremental true
        annotations "com.mybank.*"
    }
}

dependencies {
    kapt("io.micronaut.data:micronaut-data-processor")
    implementation("io.micronaut:micronaut-validation")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut:micronaut-runtime")
    implementation("javax.annotation:javax.annotation-api")
    implementation("io.micronaut:micronaut-http-client")
    implementation("io.micronaut.sql:micronaut-jdbc-hikari")
    implementation("io.micronaut.data:micronaut-data-hibernate-jpa")
    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
    runtimeOnly("com.h2database:h2")
}

mainClassName = "com.mybank.ApplicationKt"
java {
    sourceCompatibility = JavaVersion.toVersion('11')
}

compileKotlin {
    kotlinOptions {
        jvmTarget = '11'
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = '11'
    }
}

实体:

import javax.persistence.*
    
        @Entity
        @Table(name = "accounts")
        data class Account(@Id
                           //@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "account_generator")
                           //@SequenceGenerator(name = "account_generator", sequenceName = "account_seq")
                           @Column(name="ID_ACCOUNT", nullable=false)
                           //@GeneratedValue(strategy=GenerationType.SEQUENCE)
                           @GeneratedValue(strategy=GenerationType.IDENTITY)
                           var accountId: Long,
                           var name: String? = null,
        
        
                           @ManyToOne(fetch = FetchType.LAZY, optional = true)
                           @JoinColumn(name = "ACCOUNTHOLDER_ID", nullable=true, insertable=false, updatable=false)
                           val accountHolder: AccountHolder
        
        )

import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer
import java.time.LocalDate
import javax.persistence.*


@Entity
data class AccountHolder(@Id
                         //@GeneratedValue //(strategy = GenerationType.SEQUENCE, generator = "account_generator")
                         //@SequenceGenerator(name="account_generator", sequenceName = "account_seq")
                         @Column(name="accountHolderId", nullable=false)
                         //@GeneratedValue(strategy=GenerationType.SEQUENCE)
                         @GeneratedValue(strategy=GenerationType.IDENTITY)
                         var accountHolderId: Long,
                         var name: String? = null,
                         var age: Int,
                         @field:JsonFormat(pattern = "yyyy-MM-dd")
                         @field:JsonSerialize(using = LocalDateSerializer::class)
                         @field:JsonDeserialize(using = LocalDateDeserializer::class)
                         var birthDate: LocalDate,
                         var category: Category,
                         @Column(nullable = true)
                         @OneToMany(cascade = [CascadeType.ALL], fetch = FetchType.LAZY, mappedBy = "accountId")
                         val account: List<Account>? = null

)

存储库:

import com.mybank.model.AccountHolder
import io.micronaut.data.repository.CrudRepository
import io.micronaut.context.annotation.Executable
import io.micronaut.data.annotation.*

@Repository
interface AccountHolderRepository: CrudRepository<AccountHolder, Long> {
    @Executable
    fun find(name: String): AccountHolder
}

import com.mybank.model.Account
import io.micronaut.data.annotation.Repository
import io.micronaut.data.repository.CrudRepository

@Repository
interface AccountRepository : CrudRepository<Account, Long> {
}

控制器:

import com.mybank.model.Account
import com.mybank.service.AccountService
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.*

@Controller("/accounts")
class AccountController(private val accountService: AccountService)
{

    @Post
    @Consumes(MediaType.APPLICATION_JSON)
    fun addAccount(@Body account: Account): Account {
        return accountService.addAccount(account)
    }

    @Get
    fun getAccount(id: Long): Account {
        return accountService.findAccountById(id)
    }
}

import com.mybank.model.AccountHolder
import com.mybank.service.AccountHolderService
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.*

@Controller("/accountholders")
class AccountHolderController(private val accountHolderService: AccountHolderService) {

    @Post
    @Consumes(MediaType.APPLICATION_JSON)
    fun addAccountHolder(@Body accountHolder: AccountHolder): AccountHolder {
        return accountHolderService.addAccountHolder(accountHolder)
    }

    @Get
    fun getAccountHolder(id: Long): AccountHolder{
        return accountHolderService.findAccountHolderById(id)
    }
}

整个项目可以在git hub master 分支中找到

*** 杰夫建议后编辑

构建.gradle

plugins {
    id "org.jetbrains.kotlin.jvm" version "1.4.10"
    id "org.jetbrains.kotlin.kapt" version "1.4.10"
    id "org.jetbrains.kotlin.plugin.allopen" version "1.4.10"
    id "com.github.johnrengelman.shadow" version "6.1.0"
    id "io.micronaut.application" version '1.0.5'
    id "org.jetbrains.kotlin.plugin.noarg" version "1.4.10"
}

noArg {
    annotation("com.my.Annotation")
}

version "0.1"
group "com.mybank"

repositories {
    mavenCentral()
    jcenter()
}

micronaut {
    runtime "netty"
    testRuntime "junit5"
    processing {
        incremental true
        annotations "com.mybank.*"
    }
}

dependencies {
    kapt("io.micronaut.data:micronaut-data-processor")
    implementation("io.micronaut:micronaut-validation")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut:micronaut-runtime")
    implementation("javax.annotation:javax.annotation-api")
    implementation("io.micronaut:micronaut-http-client")
    implementation("io.micronaut.sql:micronaut-jdbc-hikari")
    implementation("io.micronaut.data:micronaut-data-hibernate-jpa")
    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
    runtimeOnly("com.h2database:h2")

}

mainClassName = "com.mybank.ApplicationKt"
java {
    sourceCompatibility = JavaVersion.toVersion('11')
}

compileKotlin {
    kotlinOptions {
        jvmTarget = '11'
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = '11'
    }
}

apply plugin: "kotlin-noarg"

noArg {
    annotation("com.mybank.model.NoArg")
    invokeInitializers = true
}

NoArg 类

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class NoArg

实体

@Entity
@Table(name = "accounts")
@NoArg
data class Account(@Id

也归功于其他答案,但我理解问题主题与我的完全不同:我清楚地询问了 kotlin-noarg 失败的方式,而在这个问题中,同行询问“如何在 Kotlin 中为数据类创建空构造函数安卓”

4

0 回答 0