I have a simple Entity with a single collection display.
@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; }
In my application, when I only include PagingAndSortingRepository for the meeting. I can invoke the POST command with the following payload.
{ "trackNumber" : "XYZ123", "products": [ {"model" : "MODEL", "description" : "NAME" }] }
When I add a PagingAndSortingRepository for a product and try to execute the same POST, I get the following error message.
{ "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" } }
Only with the Appointment repository do I get the next payload and can post a list of products.
{ "trackNumber" : "XYZ123", "products" : [ { "model" : "MODEL", "description" : "NAME", } ], "_links" : { "self" : { "href" : "http://localhost:8080/consumerappointment/appointments/1" } } }
source share