use of org.neo4j.ogm.context.EntityGraphMapper in project neo4j-ogm by neo4j.
the class SaveCapabilityTest method shouldSaveOnlyModifiedNodes.
@Test
public void shouldSaveOnlyModifiedNodes() {
int depth = 1;
Neo4jSession neo4jSession = (Neo4jSession) session;
CompileContext context = null;
Artist leann = new Artist("Leann Rimes");
Album lost = new Album("Lost Highway");
lost.setArtist(bonJovi);
lost.setGuestArtist(leann);
context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(lost, depth);
assertThat(context.registry()).as("Should save 3 nodes and 2 relations (5 items)").hasSize(5);
session.save(lost);
context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(lost, depth);
assertThat(context.registry()).as("Should have nothing to save").isEmpty();
session.clear();
Artist loadedLeann = session.load(Artist.class, leann.getId(), depth);
context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(loadedLeann, depth);
assertThat(context.registry()).as("Should have nothing to save").isEmpty();
loadedLeann.setName("New name");
context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(loadedLeann, depth);
assertThat(context.registry()).as("Should have one node to save").hasSize(1);
loadedLeann.getGuestAlbums().iterator().next().setName("New Album Name");
context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(loadedLeann, depth);
assertThat(context.registry()).as("Should have two node to save").hasSize(2);
}
use of org.neo4j.ogm.context.EntityGraphMapper in project neo4j-ogm by neo4j.
the class DirectRelationshipsTest method shouldBeAbleToRemoveContainedRelationshipOnly.
@Test
public void shouldBeAbleToRemoveContainedRelationshipOnly() {
// given
Folder folder = new Folder();
Document doc1 = new Document();
folder.getDocuments().add(doc1);
folder.getArchived().add(doc1);
doc1.setFolder(folder);
folder.setId(0L);
doc1.setId(1L);
mappingContext.addNodeEntity(folder);
mappingContext.addNodeEntity(doc1);
mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc1.getId(), null, Folder.class, Document.class));
mappingContext.addRelationship(new MappedRelationship(folder.getId(), "ARCHIVED", doc1.getId(), null, Folder.class, Document.class));
// when
folder.getDocuments().remove(doc1);
doc1.setFolder(null);
// then
assertThat(folder.getDocuments()).isEmpty();
assertThat(folder.getArchived()).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");
mapper = new EntityGraphMapper(mappingMetadata, mappingContext);
// There are no more changes to the graph
compiler = mapper.map(doc1).getCompiler();
compiler.useStatementFactory(new RowStatementFactory());
statements = compiler.deleteRelationshipStatements();
assertThat(statements).isEmpty();
}
Aggregations