While moving from SDN 3
to SDN 4
and from Neo4j 2.3
to Neo4j 3.0.1
I ran into a issue with inconsistency between OEM and custom Cypher queries.
I have a following Cypher query:
@Query("MATCH ()-[r]-(cg:CriterionGroup) WHERE id(cg) = {criterionGroupId} DELETE cg, r")
void deleteCriterionGroup(@Param("criterionGroupId") Long criterionGroupId);
Right now, with SDN 4 this query doesn't work without following workaround after deleteCriterionGroup
method invocation:
session.clear();
Could you please show the correct code how to delete CriterionGroup
now in SDN 4 in order to maintain the references of related nodes consistent.
This is a schema of my data:
As you can see - CriterionGroup
connected to Decision
, Criterion
and User
nodes.
UPDATED
As suggested, I have updated my method:
@Override
public void deleteCriterionGroup(Long criterionGroupId) {
CriterionGroup criterionGroup = criterionGroupRepository.findOne(criterionGroupId);
criterionGroup.setAuthor(null);
criterionGroup.setOwner(null);
criterionGroup.setCriteria(null);
criterionGroup = criterionGroupRepository.save(criterionGroup);
criterionGroupRepository.delete(criterionGroup);
}
Using the OGM to delete relationships maintains consistency, provided your object references are maintained as well. If you're deleting the CriterionGroup, then, for any related entities that are loaded by your application (Decision,Criterion,User), you will need to set their associated CriterionGroups to null and then save. If none of these were loaded (possible if you loaded the CriterionGroup to depth 0), then you can simply delete the CriterionGroup via the repository/session. Be careful though when you mix loading and saving depths.
If you want to bypass the OGM and use a custom Cypher delete statement, then, you must clear the session (or use a new one) and re-load entities to bring them back in sync with the graph.