spring - 尝试从 JHipster Kotlin 中的另一个实体引用一个 JDL 实体时抛出“指定为非空的参数为空”
问题描述
我正在尝试在 JDL Kotlin 中建立一个网站,但是 JDL 生成的实体和编辑表单存在问题。我有一个名为 的 JDL 实体Provider
,一个名为的实体Plan
需要引用它所属的提供者,以及一个名为的第三个实体PerDayPricing
,它同时引用Provider
and 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
解决方案
如果它生成数据类,那么编译器将为您生成 setter 和 getter。我想,当您想将实体设置为彼此时,您没有从异常中提供这些字段。Kotlin 默认不支持空值。因此,要么您必须提供这些字段,要么将类型标记为可空?(问号)在类型的和处。例如
var perDayPricings:String?
您应该使用给定的代码更新问题,以便我们可以看到问题所在。
推荐阅读
- r - 将点分配给 r 中的网格
- python - `conda install -c 期间的“解决环境”
tensorflow` 需要 3+ 分钟,但稍微更改名称会显着减少时间 - javascript - javascript异步:无法读取未定义的属性'then'
- algorithm - 从 a、b 和 a^n 以 p 为模计算 b^n
- android - AIDE OpenGL es 2.0 三角形不会渲染
- javascript - PHP脚本状态栏
- php - 更改 WampServer PHP Web 版本
- c - 使用多线程解析错误的参数 - c
- unity3d - 如何在我的 Unity 应用程序向用户请求权限之前添加通知?
- python-3.x - 合并多个 mp4 视频和图像