use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.
the class SaveEventDelegate method deregisterOutgoingRelationship.
private void deregisterOutgoingRelationship(Long id, String relationshipType, Class endNodeType) {
Iterator<MappedRelationship> iterator = this.registeredRelationships.iterator();
while (iterator.hasNext()) {
MappedRelationship mappedRelationship = iterator.next();
if (mappedRelationship.getStartNodeId() == id && mappedRelationship.getRelationshipType().equals(relationshipType) && endNodeType.equals(mappedRelationship.getEndNodeType())) {
deletedRelationships.add(mappedRelationship);
iterator.remove();
}
}
}
use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.
the class SaveEventDelegate method mapInstance.
// creates a MappedRelationship between the parent object and the reference. In the case that the reference
// object is a RE, the relationship is created from the start node and the end node of the RE.
// a MappedRelationship therefore represents a directed edge between two nodes in the graph.
private void mapInstance(Set<MappedRelationship> mappedRelationships, ClassInfo parentInfo, long parentId, FieldInfo reader, Object reference) {
String type = reader.relationshipType();
Direction direction = reader.relationshipDirection();
ClassInfo referenceInfo = this.session.metaData().classInfo(reference);
if (referenceInfo == null) {
return;
}
if (referenceInfo.isRelationshipEntity()) {
// The relationship entity might just get created and therefore we must be careful not to
// trigger the creation of it's id place holder, otherwise we can't check wether it's new or not.
Optional<Long> optionalReferenceId = session.context().optionalNativeId(reference);
// graph relationship is transitive across the RE domain object
Object startNode = referenceInfo.getStartNodeReader().read(reference);
ClassInfo startNodeInfo = this.session.metaData().classInfo(startNode);
Long startNodeId = session.context().nativeId(startNode);
Object endNode = referenceInfo.getEndNodeReader().read(reference);
ClassInfo endNodeInfo = this.session.metaData().classInfo(endNode);
Long endNodeId = session.context().nativeId(endNode);
MappedRelationship edge = new MappedRelationship(startNodeId, type, endNodeId, optionalReferenceId.orElse(null), startNodeInfo.getUnderlyingClass(), endNodeInfo.getUnderlyingClass());
mappedRelationships.add(edge);
} else {
// We assume the existence of the reference here
Long referenceId = session.context().nativeId(reference);
if (direction == Direction.OUTGOING) {
MappedRelationship edge = new MappedRelationship(parentId, type, referenceId, null, parentInfo.getUnderlyingClass(), referenceInfo.getUnderlyingClass());
mappedRelationships.add(edge);
} else {
MappedRelationship edge = new MappedRelationship(referenceId, type, parentId, null, referenceInfo.getUnderlyingClass(), parentInfo.getUnderlyingClass());
mappedRelationships.add(edge);
}
}
}
use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.
the class SaveEventDelegate method touched.
private Set<Object> touched() {
Set<Object> touched = new HashSet<>();
for (MappedRelationship added : addedRelationships) {
Object src = session.context().getNodeEntity(added.getStartNodeId());
Object tgt = session.context().getNodeEntity(added.getEndNodeId());
if (src != null) {
touched.add(src);
}
if (tgt != null) {
touched.add(tgt);
}
}
return touched;
}
use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.
the class SaveEventDelegate method unreachable.
private Set<Object> unreachable() {
Set<Object> unreachable = new HashSet<>();
for (MappedRelationship mappedRelationship : deletedRelationships) {
logger.debug("unreachable start {} end {}", mappedRelationship.getStartNodeId(), mappedRelationship.getEndNodeId());
addUnreachable(unreachable, mappedRelationship.getStartNodeId());
addUnreachable(unreachable, mappedRelationship.getEndNodeId());
}
return unreachable;
}
use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.
the class DirectRelationshipsTest method shouldBeAbleToRemoveAnyRegisteredRelationship.
@Test
public void shouldBeAbleToRemoveAnyRegisteredRelationship() {
// given
Folder folder = new Folder();
Document doc1 = new Document();
Document doc2 = new Document();
folder.getDocuments().add(doc1);
folder.getDocuments().add(doc2);
doc1.setFolder(folder);
doc2.setFolder(folder);
folder.setId(0L);
doc1.setId(1L);
doc2.setId(2L);
mappingContext.addNodeEntity(folder);
mappingContext.addNodeEntity(doc1);
mappingContext.addNodeEntity(doc2);
mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc1.getId(), null, Folder.class, Document.class));
mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
// when
doc2.setFolder(null);
folder.getDocuments().remove(doc2);
// then
assertThat(folder.getDocuments()).hasSize(1);
Compiler compiler = mapper.map(folder).getCompiler();
compiler.useStatementFactory(new RowStatementFactory());
List<Statement> statements = compiler.deleteRelationshipStatements();
assertThat(statements).hasSize(1);
assertThat(statements.get(0).getStatement()).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`CONTAINS`]->(endNode) DELETE rel");
// we need to re-establish the relationship in the mapping context for this expectation, otherwise
// the previous save will have de-registered the relationship.
mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
compiler = mapper.map(doc1).getCompiler();
compiler.useStatementFactory(new RowStatementFactory());
statements = compiler.deleteRelationshipStatements();
assertThat(statements).hasSize(1);
assertThat(statements.get(0).getStatement()).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`CONTAINS`]->(endNode) DELETE rel");
// we need to re-establish the relationship in the mapping context for this expectation, otherwise
// the previous save will have de-registered the relationship.
mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
compiler = mapper.map(doc2).getCompiler();
compiler.useStatementFactory(new RowStatementFactory());
statements = compiler.deleteRelationshipStatements();
assertThat(statements).hasSize(1);
assertThat(statements.get(0).getStatement()).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`CONTAINS`]->(endNode) DELETE rel");
}
Aggregations