У меня есть простая сущность с сопоставленной одной коллекцией.
@Entity
public class Appointment Identifiable<Integer> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonIgnore
private Integer id;
@Column(name="TRAK_NBR")
private String trackNumber;
@OneToMany(fetch =FetchType.EAGER, cascade= CascadeType.ALL)
@JoinColumn(name="CNSM_APT_VER_WRK_I", nullable = false)
private Set<Product> products = new HashSet<Product>();
}
@Entity
public class Product implements Identifiable<Integer> {
@Id
@Column(name = "CNSM_PRD_VER_WRK_I")
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonIgnore
private Integer id;
@Column(name = "PRD_MDL_NBR")
private String model;
@Column(name = "PRD_SPEC_DSC")
private String description;
}
В моем приложении, когда я включаю только PagingAndSortingRepository для назначения. Я могу вызвать команду POST со следующей полезной нагрузкой.
{
"trackNumber" : "XYZ123",
"products": [
{"model" : "MODEL",
"description" : "NAME"
}]
}
Когда я добавляю PagingAndSortingRepository для продукта и пробую тот же POST, я получаю следующее сообщение об ошибке.
{
"cause" : {
"cause" : {
"cause" : null,
"message" : null
},
"message" : "(was java.lang.NullPointerException) (through reference chain: com..model.Appointment[\"products\"])"
},
"message" : "Could not read JSON: (was java.lang.NullPointerException) (through reference chain: com.model.Appointment[\"products\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.model.AppointmentVerification[\"products\"])"
}
My GET payload with both Repositories returns this. This is my desired format. The link to products should be included
{
"trackNumber" : "XYZ123",
"_links" : {
"self" : {
"href" : "http://localhost:8080/consumerappointment/appointments/70"
},
"products" : {
"href" : "http://localhost:8080/consumerappointment/appointments/70/products"
}
}
Только с репозиторием встреч я получаю следующую полезную нагрузку и могу публиковать список продуктов.
{
"trackNumber" : "XYZ123",
"products" : [ {
"model" : "MODEL",
"description" : "NAME",
} ],
"_links" : {
"self" : {
"href" : "http://localhost:8080/consumerappointment/appointments/1"
}
}
}