use of org.neo4j.ogm.cypher.compiler.Compiler in project neo4j-ogm by neo4j.
the class EntityGraphMapperTest method doNothingIfNothingHasChanged.
@Test
public void doNothingIfNothingHasChanged() {
Long existingNodeId = (Long) session.query("CREATE (s:Student:DomainObject {name:'Sheila Smythe'}) RETURN id(s) AS id", emptyMap()).queryResults().iterator().next().get("id");
Student sheila = new Student();
sheila.setId(existingNodeId);
sheila.setName("Sheila Smythe");
mappingContext.addNodeEntity(sheila);
session.clear();
Compiler compiler = this.mapper.map(sheila).getCompiler();
compiler.useStatementFactory(new RowStatementFactory());
Statements cypher = new Statements(compiler.getAllStatements());
assertThat(cypher.getStatements()).isEmpty();
}
use of org.neo4j.ogm.cypher.compiler.Compiler in project neo4j-ogm by neo4j.
the class RequestExecutor method executeSave.
/**
* Execute a save request.
* Decides how the request is split depending upon characteristics of what is to be saved.
* Processes the response(s) and updates the mapping context.
*
* @param context the CompileContext for this request
*/
public void executeSave(CompileContext context) {
Compiler compiler = context.getCompiler();
compiler.useStatementFactory(new RowStatementFactory());
List<ReferenceMapping> entityReferenceMappings = new ArrayList<>();
List<ReferenceMapping> relReferenceMappings = new ArrayList<>();
boolean forceTx = compiler.updateNodesStatements().stream().anyMatch(st -> st.optimisticLockingConfig().isPresent()) || compiler.updateRelationshipStatements().stream().anyMatch(st -> st.optimisticLockingConfig().isPresent());
session.doInTransaction(() -> {
// we must create the new nodes first, and then use their node IDs when creating relationships between them
if (compiler.hasStatementsDependentOnNewNodes()) {
// execute the statements to create new nodes. The ids will be returned
// and will be used in subsequent statements that refer to these new nodes.
executeStatements(context, entityReferenceMappings, relReferenceMappings, compiler.createNodesStatements());
List<Statement> statements = new ArrayList<>();
statements.addAll(compiler.createRelationshipsStatements());
statements.addAll(compiler.updateNodesStatements());
statements.addAll(compiler.updateRelationshipStatements());
statements.addAll(compiler.deleteRelationshipStatements());
statements.addAll(compiler.deleteRelationshipEntityStatements());
executeStatements(context, entityReferenceMappings, relReferenceMappings, statements);
} else {
// only update / delete statements
List<Statement> statements = compiler.getAllStatements();
executeStatements(context, entityReferenceMappings, relReferenceMappings, statements);
}
}, forceTx, Transaction.Type.READ_WRITE);
// Update the mapping context now that the request is successful
updateNodeEntities(context, entityReferenceMappings);
updateRelationshipEntities(context, relReferenceMappings);
updateRelationships(context, relReferenceMappings);
}
use of org.neo4j.ogm.cypher.compiler.Compiler 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");
}
use of org.neo4j.ogm.cypher.compiler.Compiler in project neo4j-ogm by neo4j.
the class MergeWithPrimaryIndexTests method newNodeUsesGraphIdWhenPrimaryIndexNotPresent.
@Test
public void newNodeUsesGraphIdWhenPrimaryIndexNotPresent() {
Pizza pizza = new Pizza("Plain");
assertThat(pizza.getId()).isNull();
Compiler compiler = mapAndCompile(pizza);
assertThat(compiler.hasStatementsDependentOnNewNodes()).isFalse();
assertThat(compiler.createNodesStatements().get(0).getStatement()).isEqualTo("UNWIND $rows as row CREATE (n:`Pizza`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
}
use of org.neo4j.ogm.cypher.compiler.Compiler in project neo4j-ogm by neo4j.
the class MergeWithPrimaryIndexTests method newNodeUsesPrimaryIndexWhenPresent.
@Test
public void newNodeUsesPrimaryIndexWhenPresent() {
User newUser = new User("bachmania", "Michal Bachman", "password");
Compiler compiler = mapAndCompile(newUser);
assertThat(compiler.hasStatementsDependentOnNewNodes()).isFalse();
assertThat(compiler.createNodesStatements().get(0).getStatement()).isEqualTo("UNWIND $rows as row MERGE (n:`User`{login: row.props.login}) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
}
Aggregations