I have the following node class:
@Node
@Data
public class Graphic {
@Id
@GeneratedValue(generatorClass = UUIDStringGenerator.class)
private String id;
private String title;
@Relationship(type = "CONTAINS")
private List<Field> fields;
}
A field has multiple properties (id, title etc). What is the best way in Spring Data to update these fields (create new ones if they don't have an id, update existing and delete these that are not in the payload)? I would like to update the whole Graphic entity including its fields with just one request.
I tried this:
MATCH (infographic:Infographic {id: $id})
SET infographic.title = $payload.title // works
WITH infographic
UNWIND $payload.fields as payloadField // doesn't work
...
This half ways worked but obviously doesn't delete the fields that are not in the payload. Is there some pattern that I could use for this?
Edit: I got it working with a pretty bad workaround:
List<String> existingFieldIds = this.getGraphicById(id).getFields().stream().map(Graphic::getId).toList();
List<String> savedFieldIds = graphicRepository.update(id, Values.value(ClassToMapConverter.toMap(payload)));
List<String> fieldIdsToDelete = existingFieldIds.stream().filter(initial -> !savedFieldIds.contains(initial)).toList();
this.graphicFieldRepository.deleteAllById(fieldIdsToDelete);
Though this makes a lot of additional DB calls which I would like to prevent