Neo4j wie man Knoten rekursiv von einem Startknoten löscht
In meiner Neo4j-Datenbank habe ich folgende Entitäten:
@NodeEntity
public class Product {
private final static String CONTAINS = "CONTAINS";
private final static String DEFINED_BY = "DEFINED_BY";
private final static String VOTED_FOR = "VOTED_FOR";
private final static String PARENT = "PARENT";
private final static String CREATED_BY = "CREATED_BY";
@GraphId
private Long id;
@RelatedTo(type = PARENT, direction = Direction.INCOMING)
private Product parent;
@RelatedTo(type = CONTAINS, direction = Direction.OUTGOING)
private Set<Product> childProducts = new HashSet<>();
@RelatedTo(type = DEFINED_BY, direction = Direction.INCOMING)
private Set<Criterion> criterias = new HashSet<>();
@RelatedTo(type = VOTED_FOR, direction = Direction.INCOMING)
private Set<Vote> votes = new HashSet<>();
@RelatedTo(type = CREATED_BY, direction = Direction.OUTGOING)
private User user;
}
@NodeEntity
public class Criterion {
private final static String CREATED_BY = "CREATED_BY";
private final static String DEFINED_BY = "DEFINED_BY";
@GraphId
private Long id;
@RelatedTo(type = DEFINED_BY, direction = Direction.OUTGOING)
private Product owner;
@RelatedTo(type = CREATED_BY, direction = Direction.OUTGOING)
private User user;
}
@NodeEntity
public class Vote {
private static final String VOTED_ON = "VOTED_ON";
private final static String VOTED_FOR = "VOTED_FOR";
private static final String CREATED_BY = "CREATED_BY";
@GraphId
private Long id;
@RelatedTo(type = VOTED_FOR, direction = Direction.OUTGOING)
private Product product;
@RelatedTo(type = VOTED_ON, direction = Direction.OUTGOING)
private Criterion criterion;
@RelatedTo(type = CREATED_BY, direction = Direction.OUTGOING)
private User user;
}
Product
ist eine zusammengesetzte Entität und kann child @ enthaltProducts
. Ausgehend von einigenProduct
Knoten in der Hierarchie muss ich alle @ löschVotes
auf diesemProduct
und dann muss ich rekursiv alle untergeordneten @ löschProducts
, Criteria
definiert durch diese Knoten undVotes
. User
Knoten dürfen nicht gelöscht werden.
Ich habe diese Cypher-Abfrage versucht:
MATCH (p:Product)-[r:CONTAINS*]-(e) WHERE id(p) = {productId} FOREACH (rel IN r| DELETE rel) DELETE e
, löscht jedoch nur Produkte und löscht keine Stimmen auf dem Startknoten und alle untergeordneten Kriterien und Stimmen. Bitte helfen Sie mir bei einer korrekten Cypher-Abfrage. Vielen Dank