0

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:

enter image description here

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);
}
brunoid
  • 2,121
  • 2
  • 12
  • 22
  • It is pretty normal, the OGM is not aware of the changes you do in the database if you don't tell him. Also bear in mind that sdn3 was built around an embedded gdb, while sdn4 is built around a server database and has a real concept of data mapping – Christophe Willemsen May 29 '16 at 21:25
  • This is my question was - what is the correct way to delete this node with OGM ? I don't want to use workarounds. – brunoid May 29 '16 at 21:29
  • 1
    load the criterionGroup and his relationships, remove the relationships with your POJO's, session.save(); – Christophe Willemsen May 29 '16 at 21:40
  • Thanks. I have updated my method. Is it a correct way to go ? – brunoid May 29 '16 at 21:50
  • 1
    It seems like, maybe make sure that there is no more reference from Author to CriterionGroup for eg, at the object level I mean. – Christophe Willemsen May 29 '16 at 22:10

1 Answers1

2

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.

Luanne
  • 19,145
  • 1
  • 39
  • 51