Search in sources :

Example 16 with MappedRelationship

use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.

the class CompilerTest method shouldUpdatingExistingRelationshipEntity.

@Test
public void shouldUpdatingExistingRelationshipEntity() {
    Long forumId = 0L;
    Long topicId = 1L;
    Long relationshipId = 2L;
    Forum forum = new Forum();
    forum.setId(forumId);
    forum.setName("Spring Data Neo4j");
    Topic topic = new Topic();
    topic.setTopicId(topicId);
    topic.setInActive(Boolean.FALSE);
    ForumTopicLink link = new ForumTopicLink();
    link.setId(relationshipId);
    link.setForum(forum);
    link.setTopic(topic);
    forum.setTopicsInForum(Arrays.asList(link));
    mappingContext.addNodeEntity(forum);
    mappingContext.addNodeEntity(topic);
    mappingContext.addRelationshipEntity(link, relationshipId);
    MappedRelationship mappedRelationship = new MappedRelationship(forumId, "HAS_TOPIC", topicId, relationshipId, Forum.class, ForumTopicLink.class);
    mappingContext.addRelationship(mappedRelationship);
    // change the timestamp
    link.setTimestamp(327790L);
    // expect the property on the relationship entity to be updated on the graph relationship
    Compiler compiler = mapAndCompile(link, -1);
    List<Statement> statements = compiler.createNodesStatements();
    assertThat(statements).isEmpty();
    statements = compiler.updateRelationshipStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows AS row MATCH ()-[r]->() WHERE ID(r) = row.relId SET r += row.props RETURN ID(r) as ref, ID(r) as id, $type as type");
    assertThat((List) statements.get(0).getParameters().get("rows")).hasSize(1);
}
Also used : Statement(org.neo4j.ogm.request.Statement) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) ForumTopicLink(org.neo4j.ogm.domain.forum.ForumTopicLink) ArrayList(java.util.ArrayList) List(java.util.List) Topic(org.neo4j.ogm.domain.forum.Topic) Forum(org.neo4j.ogm.domain.forum.Forum) Test(org.junit.Test)

Example 17 with MappedRelationship

use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.

the class CompilerTest method addObjectToExistingCollection.

@Test
public void addObjectToExistingCollection() {
    // create
    Long wallerId = 0L;
    Long maryId = 1L;
    School waller = new School("Waller");
    waller.setId(wallerId);
    Teacher mary = new Teacher("Mary");
    mary.setId(maryId);
    // relate
    mary.setSchool(waller);
    // validate the domain model
    assertThat(mary.getSchool()).isEqualTo(waller);
    assertThat(waller.getTeachers()).contains(mary);
    assertThat(waller.getTeachers()).hasSize(1);
    // set the mapping context accordingly
    mappingContext.addNodeEntity(mary);
    mappingContext.addNodeEntity(waller);
    mappingContext.addRelationship(new MappedRelationship(maryId, "SCHOOL", wallerId, null, Teacher.class, School.class));
    mappingContext.addRelationship(new MappedRelationship(wallerId, "TEACHERS", maryId, null, School.class, Teacher.class));
    Teacher jim = new Teacher("Jim");
    jim.setSchool(waller);
    assertThat(waller.getTeachers()).contains(jim);
    assertThat(waller.getTeachers()).hasSize(2);
    assertThat(jim.getSchool()).isEqualTo(waller);
    // Save jim
    Compiler compiler = mapAndCompile(jim, -1);
    List<Statement> createNodesStatements = compiler.createNodesStatements();
    assertThat(createNodesStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Teacher`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    assertThat(createNodesStatements).extracting(Statement::getParameters);
    for (Statement statement : createNodesStatements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    List<Statement> createRelsStatements = compiler.createRelationshipsStatements();
    assertThat(createRelsStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`SCHOOL`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type", "UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`TEACHERS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    // Save waller
    compiler = mapAndCompile(waller, -1);
    createNodesStatements = compiler.createNodesStatements();
    assertThat(createNodesStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Teacher`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    for (Statement statement : createNodesStatements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    createRelsStatements = compiler.createRelationshipsStatements();
    assertThat(createRelsStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`SCHOOL`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type", "UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`TEACHERS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    // Save mary
    compiler = mapAndCompile(mary, -1);
    createNodesStatements = compiler.createNodesStatements();
    assertThat(createNodesStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Teacher`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    for (Statement statement : createNodesStatements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    createRelsStatements = compiler.createRelationshipsStatements();
    assertThat(createRelsStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`SCHOOL`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type", "UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`TEACHERS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
}
Also used : School(org.neo4j.ogm.domain.education.School) Statement(org.neo4j.ogm.request.Statement) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) Teacher(org.neo4j.ogm.domain.education.Teacher) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 18 with MappedRelationship

use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.

the class CompilerTest method shouldCorrectlyRemoveRelationshipWhenItemIsRemovedFromCollection.

@Test
public void shouldCorrectlyRemoveRelationshipWhenItemIsRemovedFromCollection() {
    // simple music course with three students
    Long mid = 0L;
    Long xid = 1L;
    Long yid = 2L;
    Long zid = 3L;
    Course music = new Course("GCSE Music");
    music.setId(mid);
    Student xavier = new Student("xavier");
    xavier.setId(xid);
    Student yvonne = new Student("Yvonne");
    yvonne.setId(yid);
    Student zack = new Student("Zack");
    zack.setId(zid);
    music.setStudents(Arrays.asList(yvonne, xavier, zack));
    mappingContext.addRelationship(new MappedRelationship(mid, "STUDENTS", xid, null, Course.class, Student.class));
    mappingContext.addRelationship(new MappedRelationship(mid, "STUDENTS", yid, null, Course.class, Student.class));
    mappingContext.addRelationship(new MappedRelationship(mid, "STUDENTS", zid, null, Course.class, Student.class));
    mappingContext.addNodeEntity(xavier);
    mappingContext.addNodeEntity(yvonne);
    mappingContext.addNodeEntity(zack);
    mappingContext.addNodeEntity(music);
    // now, update the domain model, setting yvonne as the only music student (i.e remove zack and xavier)
    music.setStudents(Arrays.asList(yvonne));
    // Save music
    Compiler compiler = mapAndCompile(music, -1);
    assertThat(compiler.createNodesStatements()).isEmpty();
    assertThat(compiler.createRelationshipsStatements()).isEmpty();
    List<Statement> deleteRelsStatement = compiler.deleteRelationshipStatements();
    assertThat(deleteRelsStatement).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`STUDENTS`]->(endNode) DELETE rel");
    assertThat(((List) deleteRelsStatement.get(0).getParameters().get("rows"))).hasSize(2);
}
Also used : Statement(org.neo4j.ogm.request.Statement) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) Course(org.neo4j.ogm.domain.education.Course) Student(org.neo4j.ogm.domain.education.Student) Test(org.junit.Test)

Aggregations

MappedRelationship (org.neo4j.ogm.context.MappedRelationship)18 Test (org.junit.Test)11 Statement (org.neo4j.ogm.request.Statement)9 Teacher (org.neo4j.ogm.domain.education.Teacher)4 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Compiler (org.neo4j.ogm.cypher.compiler.Compiler)3 School (org.neo4j.ogm.domain.education.School)3 Document (org.neo4j.ogm.domain.filesystem.Document)3 Folder (org.neo4j.ogm.domain.filesystem.Folder)3 HashSet (java.util.HashSet)2 Course (org.neo4j.ogm.domain.education.Course)2 Student (org.neo4j.ogm.domain.education.Student)2 Forum (org.neo4j.ogm.domain.forum.Forum)2 ForumTopicLink (org.neo4j.ogm.domain.forum.ForumTopicLink)2 Topic (org.neo4j.ogm.domain.forum.Topic)2 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)2 Direction (org.neo4j.ogm.annotation.Relationship.Direction)1 EntityGraphMapper (org.neo4j.ogm.context.EntityGraphMapper)1