首页 > 解决方案 > kotlin 中的 JPA 错误:“学生”类应该有 [public, protected] no-arg 构造函数

问题描述

有谁知道我该如何解决这个问题:'班级'学生'应该有[公共,受保护的]无参数构造函数'?

它在抱怨与SchoolLesson

import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.JoinColumn
import javax.persistence.ManyToOne


@Entity
data class Student(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Long = -1,

    @ManyToOne
    @NotNull
    @JoinColumn(name = "school_lesson_id", referencedColumnName = "id")
    val report: SchoolLesson,

)

#EDIT 应要求添加了 SchoolLesson

    import javax.persistence.*
    import javax.validation.constraints.NotNull

    @Entity
    data class SchoolLesson(
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(nullable = false)
      val id: Long = -1,
    
      @NotNull
      val name: String = "",
    )

标签: kotlinjpa

解决方案


您可以使用无参数编译器插件,它将添加“一个额外的零参数构造函数”。详细信息在链接中。


推荐阅读