InstantiationException:在 Kotlin 中使实体字段不可为空时,没有实体的默认构造函数

Posted

技术标签:

【中文标题】InstantiationException:在 Kotlin 中使实体字段不可为空时,没有实体的默认构造函数【英文标题】:InstantiationException: No default constructor for entity when making an Entity field NOT nullable in Kotlin 【发布时间】:2020-05-03 07:46:24 【问题描述】:

我有一个项目实体(如下所示)。当我尝试改变时

var fullName: String?= null

到:

var fullName: String,

我突然从 Hibernate 得到一个 InstantiationException:

org.hibernate.InstantiationException: No default constructor for entity:

在此之前没有任何问题。可能出了什么问题?这是实体类:

package no.inmeta.ris.project.model.entity

import org.hibernate.annotations.GenericGenerator
import java.util.*
import javax.persistence.*

@Entity
data class Project (

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(columnDefinition = "BINARY(16)")
    var id : UUID? = null,

    var fullName: String,

    var shortName: String? = null,

    var prevRefId: String? = null,

    var startDate: Date? = null,

    var endDate: Date?  = null,

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "Associated_Projects",
        joinColumns = [JoinColumn(name = "associated_project")],
        inverseJoinColumns = [JoinColumn(name = "second_associated_project") ])
    var associatedProjects: List<Project?> = mutableListOf(),

    var shortDescription: String? = null,

    var summary: String?  = null,

    var url: String? = null,

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "Project_Types",
        joinColumns = [JoinColumn(name = "project_id")],
        inverseJoinColumns = [JoinColumn(name = "type_id") ])
    var type: List<Type?> = mutableListOf()
    )

【问题讨论】:

嘿@Nora,我认为缺少默认构造函数。你添加了吗? 【参考方案1】:

为 fullName 参数提供默认值或保持为空。

在您的代码中,它没有默认值,因此在生成的 Java 字节码 Project 类中不会有没有参数的构造函数 - 它至少需要 1 个参数来初始化 fullName 字段。

【讨论】:

我正在创建一个 API 并试图避免任何默认值。同时我不希望 fullName 为空,因为它是必填字段 Hibernate 需要一个默认构造函数。默认构造函数是没有参数的构造函数。如果希望 Kotlin 为数据类生成默认构造函数,则必须为其主构造函数中声明的每个属性设置默认参数。 看这里的答案***.com/questions/37873995/…【参考方案2】:

您应该提供一个默认构造函数。如果您不想在此类之外公开它,您可以将其设为私有:

package no.inmeta.ris.project.model.entity

import org.hibernate.annotations.GenericGenerator
import java.util.*
import javax.persistence.*

@Entity
data class Project (

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(columnDefinition = "BINARY(16)")
    var id : UUID? = null,

    var fullName: String,

    var shortName: String? = null,

    var prevRefId: String? = null,

    var startDate: Date? = null,

    var endDate: Date?  = null,

    private constructor()

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "Associated_Projects",
        joinColumns = [JoinColumn(name = "associated_project")],
        inverseJoinColumns = [JoinColumn(name = "second_associated_project") ])
    var associatedProjects: List<Project?> = mutableListOf(),

    var shortDescription: String? = null,

    var summary: String?  = null,

    var url: String? = null,

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "Project_Types",
        joinColumns = [JoinColumn(name = "project_id")],
        inverseJoinColumns = [JoinColumn(name = "type_id") ])
    var type: List<Type?> = mutableListOf()
    )

【讨论】:

以上是关于InstantiationException:在 Kotlin 中使实体字段不可为空时,没有实体的默认构造函数的主要内容,如果未能解决你的问题,请参考以下文章

java.lang.InstantiationException: entity.User 实例化异常解决

MultipartHttpServletRequest BeanInstantiationException 根本原因 java.lang.InstantiationException

Play 2.4 WebSocket在使用编译时依赖注入时抛出InstantiationException

Xamarin Android java.lang.InstantiationException:java.lang.Class 没有零参数构造函数

firebase:InstantiationException:无法实例化抽象类 java.util.TimeZone

java.lang.IllegalStateException:在 JSF 中实现自定义标记处理程序时出现 java.lang.InstantiationException