0

I'm trying to build a website in JDL Kotlin, but am having a problem with the JDL generated entities and edit forms. I have a JDL entity called Provider, an entity called Plan that needs to reference which provider it belongs to, and a third entity called PerDayPricing that references both Provider and Plan, all as per the following JDL configuration:

entity Provider {
    // [...]
}

entity Plan {
    // [...]
}

entity PerDayPricing {
    // [...]
}

relationship OneToMany {
    Provider to Plan
}

relationship OneToMany {
    Provider to PerDayPricing
    Plan to PerDayPricing
}

service all with serviceImpl

However, when I try to create or set the Provider field on a Plan item, it spits out the following error:

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 is referenced here even though I didn't change any of its items. When I do try to set the Provider field on a PerDayPricing item, it gives the following error:

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"])]

I actually have no idea what's going on here as I don't have a lot of experience with JHipster. I simply imported the JDL source file and let JHipster create files based on its configuration, without changing any of them, and this is already happening. References to the method names setPlans and setPerDayPricings don't even exist in the codebase, so I'm assuming they are being generated by Kotlin in the background?

Does anyone know what's going on and how I can fix it?

Edit: The following are the signatures of the entity classes (regular fields were removed for brevity):

// 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

1 Answers1

2

If it generates data classes, then compiler will generate you the setters and getters. I suppose, when you want to set the entity to one another, you did not provide those fields from the exception. Kotlin not support null values by default. So either you have to provide those fields or mark the type as nullable with ? (question mark) at the and of the type. For example

var perDayPricings:String?

You should update the question with the given code, so we can see what is the problem.

zlaval
  • 1,941
  • 1
  • 10
  • 11
  • The problem is that your request's json does not countains the given set's data (for example perDayPricings). It is missing or null in json? Anyway you should provide empty array in the json or modify the object mapper to handle missing and null values in your way. There is a third one, but i dont suggest to use because it is ugly and error phrone, but you can mark the whole set as nullable: `var perDayPricings: MutableSet?` – zlaval Jun 04 '20 at 07:22
  • I would assume that the Java version would just have all the parameters nullable, right? – StaticallyTypedRice Jun 05 '20 at 00:52
  • yes, kotlin's default is that all types are non-nullable, java's types are optional by default – zlaval Jun 05 '20 at 08:41