I have this entity:
@Entity
public class Rating {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Boolean isNa = false;
private String comment = "";
@Min(0)
@Max(5)
private Integer points;
@ManyToOne
@JoinColumn(name="audit_id", nullable=false)
private Audit audit;
@ManyToOne
@JoinColumn(name="question_id", nullable=false)
private Question question;
// ....
}
when I perform a JsonPatch in a PatchMapping, the fields autdit and question are set to null, which in turn ensures that I cannot save the rating.
Here is my PatchMapping:
@PatchMapping("/api/v1/ratings/{id}")
public ResponseEntity<Rating> updateRating(@PathVariable("id") long id, @RequestBody JsonMergePatch patch) throws ChangeSetPersister.NotFoundException, JsonPatchException, JsonProcessingException {
Rating entity = findRatingService.findRatingById(id).orElseThrow(ChangeSetPersister.NotFoundException::new);
JsonNode entityJsonNode = objectMapper.convertValue(entity, JsonNode.class);
JsonNode patchedEntityJsonNode = patch.apply(entityJsonNode);
Rating patchedEntity = objectMapper.treeToValue(patchedEntityJsonNode, Rating.class);
Rating updatedEntity = saveRatingService.saveRating(patchedEntity);
return ResponseEntity.ok(updatedEntity);
}
patchedEntity.audit
is allways null
My Example Request Body:
[
{
"op": "replace",
"value": "foo",
"path": "/comment"
}
]
I have also tried slightly modified forms in the controller. But I am slightly stuck here and would appreciate any advice.
I’ve tried JsonPatch
instead of JsonMergePatch