Search in sources :

Example 6 with EntityGraphMapper

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);
}
Also used : Artist(org.neo4j.ogm.domain.music.Artist) EntityGraphMapper(org.neo4j.ogm.context.EntityGraphMapper) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Album(org.neo4j.ogm.domain.music.Album) CompileContext(org.neo4j.ogm.cypher.compiler.CompileContext) Test(org.junit.Test)

Example 7 with EntityGraphMapper

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();
}
Also used : EntityGraphMapper(org.neo4j.ogm.context.EntityGraphMapper) Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Statement(org.neo4j.ogm.request.Statement) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) Folder(org.neo4j.ogm.domain.filesystem.Folder) Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Aggregations

EntityGraphMapper (org.neo4j.ogm.context.EntityGraphMapper)7 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)3 Before (org.junit.Before)2 Test (org.junit.Test)2 EntityMapper (org.neo4j.ogm.context.EntityMapper)2 MappingContext (org.neo4j.ogm.context.MappingContext)2 ArrayList (java.util.ArrayList)1 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)1 CompileContext (org.neo4j.ogm.cypher.compiler.CompileContext)1 Compiler (org.neo4j.ogm.cypher.compiler.Compiler)1 Document (org.neo4j.ogm.domain.filesystem.Document)1 Folder (org.neo4j.ogm.domain.filesystem.Folder)1 Album (org.neo4j.ogm.domain.music.Album)1 Artist (org.neo4j.ogm.domain.music.Artist)1 MetaData (org.neo4j.ogm.metadata.MetaData)1 Statement (org.neo4j.ogm.request.Statement)1 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)1 SessionFactory (org.neo4j.ogm.session.SessionFactory)1