首页 > 解决方案 > 尝试从 JHipster Kotlin 中的另一个实体引用一个 JDL 实体时抛出“指定为非空的参数为空”

问题描述

我正在尝试在 JDL Kotlin 中建立一个网站,但是 JDL 生成的实体和编辑表单存在问题。我有一个名为 的 JDL 实体Provider,一个名为的实体Plan需要引用它所属的提供者,以及一个名为的第三个实体PerDayPricing,它同时引用Providerand Plan,所有这些都根据以下 JDL 配置:

entity Provider {
    // [...]
}

entity Plan {
    // [...]
}

entity PerDayPricing {
    // [...]
}

relationship OneToMany {
    Provider to Plan
}

relationship OneToMany {
    Provider to PerDayPricing
    Plan to PerDayPricing
}

service all with serviceImpl

但是,当我尝试在计划项上创建或设置提供者字段时,会出现以下错误:

WARN [PID] --- [ XNIO-1 task-10] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageConversionException: JSON conversion problem: Parameter specified as non-null is null: method com.example.project.domain.Plan.setPerDayPricings, parameter <set-?>; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Parameter specified as non-null is null: method com.example.project.domain.Plan.setPerDayPricings, parameter <set-?> (through reference chain: com.example.project.domain.Plan["perDayPricings"])]

PerDayPricing即使我没有更改它的任何项目,这里也引用了它。当我尝试在 PerDayPricing 项目上设置 Provider 字段时,会出现以下错误:

WARN [PID] --- [ XNIO-1 task-32] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageConversionException: JSON conversion problem: Parameter specified as non-null is null: method com.example.project.domain.Provider.setPlans, parameter <set-?>; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Parameter specified as non-null is null: method com.example.project.domain.Provider.setPlans, parameter <set-?> (through reference chain: com.example.project.domain.PerDayPricing["provider"]->com.example.project.domain.Provider["plans"])]

我实际上不知道这里发生了什么,因为我对 JHipster 没有太多经验。我只是导入了 JDL 源文件并让 JHipster 根据其配置创建文件,而不更改任何文件,这已经发生了。对方法名称的引用setPlans甚至setPerDayPricings不存在于代码库中,所以我假设它们是由 Kotlin 在后台生成的?

有谁知道发生了什么以及我该如何解决?

编辑:以下是实体类的签名(为简洁起见,删除了常规字段):

// src/main/kotlin/domain/Provider.kt

@Entity
@Table(name = "plan")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
data class Plan(
    // [...]

    @OneToMany(mappedBy = "plan")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    var perDayPricings: MutableSet<PerDayPricing> = mutableSetOf(),

    @OneToMany(mappedBy = "plan")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    var fixedDurationPricings: MutableSet<FixedDurationPricing> = mutableSetOf(),

    @ManyToOne @JsonIgnoreProperties("plans")
    var provider: Provider? = null

    // jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
) : Serializable
// src/main/kotlin/domain/Plan.kt

@Entity
@Table(name = "provider")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
data class Provider(
    // [...]

    @OneToMany(mappedBy = "provider")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    var plans: MutableSet<Plan> = mutableSetOf(),

    @OneToMany(mappedBy = "provider")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    var perDayPricings: MutableSet<PerDayPricing> = mutableSetOf(),

    @OneToMany(mappedBy = "provider")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    var fixedDurationPricings: MutableSet<FixedDurationPricing> = mutableSetOf()

    // jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
) : Serializable
// src/main/kotlin/domain/PerDayPricing.kt

/**
 * A PerDayPricing.
 */
@Entity
@Table(name = "per_day_pricing")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
data class PerDayPricing(
    // [...]

    @ManyToOne @JsonIgnoreProperties("perDayPricings")
    var provider: Provider? = null,

    @ManyToOne @JsonIgnoreProperties("perDayPricings")
    var plan: Plan? = null

    // jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
) : Serializable

标签: springkotlinjhipsterjdl

解决方案


如果它生成数据类,那么编译器将为您生成 setter 和 getter。我想,当您想将实体设置为彼此时,您没有从异常中提供这些字段。Kotlin 默认不支持空值。因此,要么您必须提供这些字段,要么将类型标记为可空?(问号)在类型的和处。例如

var perDayPricings:String?

您应该使用给定的代码更新问题,以便我们可以看到问题所在。


推荐阅读