use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.
the class EntityGraphMapperTest method shouldCorrectlyRemoveRelationshipWhenItemIsMovedToDifferentCollection.
@Test
public void shouldCorrectlyRemoveRelationshipWhenItemIsMovedToDifferentCollection() {
// start with one teacher teachers two courses, each with one student in
Iterable<Map<String, Object>> executionResult = session.query("CREATE (t:Teacher {name:'Ms Thompson'}), " + "(bs:Course {name:'GNVQ Business Studies'})-[:STUDENTS]->(s:Student:DomainObject {name:'Shivani'}), " + "(dt:Course {name:'GCSE Design & Technology'})-[:STUDENTS]->(j:Student:DomainObject {name:'Jeff'}), " + "(t)-[:COURSES]->(bs), (t)-[:COURSES]->(dt) " + "RETURN id(t) AS teacher_id, id(bs) AS bs_id, id(dt) AS dt_id, id(s) AS s_id", emptyMap()).queryResults();
Map<String, ?> results = executionResult.iterator().next();
Long teacherId = (Long) results.get("teacher_id");
Long businessStudiesCourseId = (Long) results.get("bs_id");
Long designTechnologyCourseId = (Long) results.get("dt_id");
Long studentId = (Long) results.get("s_id");
session.clear();
Course designTech = session.load(Course.class, designTechnologyCourseId);
Course businessStudies = session.load(Course.class, businessStudiesCourseId);
Teacher msThompson = session.load(Teacher.class, teacherId);
Student shivani = session.load(Student.class, studentId);
// move student from one course to the other
businessStudies.setStudents(Collections.<Student>emptyList());
designTech.getStudents().add(shivani);
session.save(msThompson);
session.clear();
assertThat(session.query("MATCH (t:Teacher {name:'Ms Thompson'}), " + "(bs:Course {name:'GNVQ Business Studies'}), (dt:Course {name:'GCSE Design & Technology'}) " + "WHERE (dt)-[:STUDENTS]->(:Student:DomainObject {name:'Jeff'}) and " + "(dt)-[:STUDENTS]->(:Student:DomainObject {name:'Shivani'}) and " + "(t)-[:COURSES]->(bs) and (t)-[:COURSES]->(dt) return t, bs, dt", emptyMap()).queryResults()).hasSize(1);
}
use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.
the class EntityGraphMapperTest method shouldNotGetIntoAnInfiniteLoopWhenSavingObjectsThatReferenceEachOther.
@Test
public void shouldNotGetIntoAnInfiniteLoopWhenSavingObjectsThatReferenceEachOther() {
Teacher missJones = new Teacher("Miss Jones");
Teacher mrWhite = new Teacher("Mr White");
School school = new School("Hilly Fields");
school.setTeachers(Arrays.asList(missJones, mrWhite));
session.save(school);
session.clear();
assertThat(session.query("MATCH (j:Teacher {name:'Miss Jones'}), " + "(w:Teacher {name:'Mr White'}), " + "(s:School:DomainObject {name:'Hilly Fields'}) " + "WHERE (s)-[:TEACHERS]->(j)-[:SCHOOL]->(s) and " + "(s)-[:TEACHERS]->(w)-[:SCHOOL]->(s) return j, w, s", emptyMap()).queryResults()).hasSize(1);
}
use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.
the class EntityGraphMapperTest method shouldNotMessUpMixedTypedLists.
// GH-821
@Test
public void shouldNotMessUpMixedTypedLists() {
Teacher jim = new Teacher("Jim");
sessionFactory.openSession().save(jim);
Result result = session.query("MATCH (n:Teacher) RETURN n, [[0,1,2], [[\"a\",\"b\"]], [\"c\"], \"d\"] as nested_list", Collections.emptyMap());
assertThat(result).hasSize(1).first().satisfies(row -> {
Object nestedLists = row.get("nested_list");
assertThat(nestedLists).isInstanceOf(Object[].class);
Object[] columns = (Object[]) nestedLists;
assertThat(columns).hasSize(4);
assertThat(columns[0]).isInstanceOf(Long[].class).satisfies(c -> assertThat(((Long[]) c)).containsExactly(0L, 1L, 2L));
assertThat(columns[1]).isInstanceOf(String[][].class).satisfies(c -> assertThat(((String[][]) c)[0]).containsExactly("a", "b"));
assertThat(columns[2]).isInstanceOf(String[].class).satisfies(c -> assertThat(((String[]) c)).containsExactly("c"));
assertThat(columns[3]).isInstanceOf(String.class).isEqualTo("d");
assertThat(row.get("n")).isInstanceOf(Teacher.class).extracting("name").first().isEqualTo("Jim");
});
}
use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.
the class CompilerTest method shouldCorrectlyRemoveRelationshipWhenItemIsDisconnectedFromNonOwningSide.
@Test
public void shouldCorrectlyRemoveRelationshipWhenItemIsDisconnectedFromNonOwningSide() {
Long schoolId = 0L;
Long whiteId = 1L;
Long jonesId = 2L;
School hillsRoad = new School("Hills Road Sixth Form College");
hillsRoad.setId(schoolId);
Teacher mrWhite = new Teacher("Mr White");
mrWhite.setId(whiteId);
Teacher missJones = new Teacher("Miss Jones");
missJones.setId(jonesId);
hillsRoad.setTeachers(Arrays.asList(missJones, mrWhite));
assertThat(hillsRoad.getTeachers()).contains(mrWhite);
assertThat(hillsRoad.getTeachers()).contains(missJones);
assertThat(mrWhite.getSchool()).isEqualTo(hillsRoad);
assertThat(missJones.getSchool()).isEqualTo(hillsRoad);
mappingContext.addNodeEntity(hillsRoad);
mappingContext.addNodeEntity(mrWhite);
mappingContext.addNodeEntity(missJones);
mappingContext.addRelationship(new MappedRelationship(schoolId, "TEACHERS", whiteId, null, School.class, Teacher.class));
mappingContext.addRelationship(new MappedRelationship(schoolId, "TEACHERS", jonesId, null, School.class, Teacher.class));
mappingContext.addRelationship(new MappedRelationship(whiteId, "SCHOOL", schoolId, null, Teacher.class, School.class));
mappingContext.addRelationship(new MappedRelationship(jonesId, "SCHOOL", schoolId, null, Teacher.class, School.class));
// Fire Mr White:
mrWhite.setSchool(null);
// validate model:
assertThat(mrWhite.getSchool()).isNull();
assertThat(hillsRoad.getTeachers()).doesNotContain(mrWhite);
// we expect hillsRoad relationship to mrWhite to be removed.
// however, the change to MrWhite's relationship is not detected.
// this is because MrWhite is not "visited" during the traversal of
// hillsRoad - his reference is now inaccessible. this looks like a FIXME
Compiler compiler = mapAndCompile(hillsRoad, -1);
List<Statement> statements = compiler.createNodesStatements();
assertThat(statements).isEmpty();
statements = compiler.createRelationshipsStatements();
assertThat(statements).isEmpty();
statements = compiler.deleteRelationshipStatements();
assertThat(statements).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:`TEACHERS`]->(endNode) DELETE rel");
assertThat(((List) statements.get(0).getParameters().get("rows"))).hasSize(1);
// we expect mrWhite's relationship to hillsRoad to be removed
// but the change to hillsRoad's relationship with MrWhite is not detected
// this is because hillsRoad object is no longer directly accessible from MrWhite
// looks like a FIXME (infer symmetric deletions)
compiler = mapAndCompile(mrWhite, -1);
statements = compiler.createNodesStatements();
assertThat(statements).isEmpty();
statements = compiler.createRelationshipsStatements();
assertThat(statements).isEmpty();
statements = compiler.deleteRelationshipStatements();
assertThat(statements).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:`SCHOOL`]->(endNode) DELETE rel");
assertThat(((List) statements.get(0).getParameters().get("rows"))).hasSize(1);
// because missJones has a reference to hillsRoad, we expect an outcome
// the same as if we had saved hillsRoiad directly.
// expectOnSave(missJones,
// "MATCH ($0)-[_2:TEACHERS]->($1) WHERE id($0)=0 AND id($1)=1 DELETE _2");
}
use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.
the class CompilerTest method createSimpleRelationshipsBetweenObjects.
@Test
public void createSimpleRelationshipsBetweenObjects() {
School waller = new School("Waller");
Teacher mary = new Teacher("Mary");
mary.setSchool(waller);
waller.getTeachers().add(mary);
Compiler compiler = mapAndCompile(waller, -1);
compiler.useStatementFactory(new RowStatementFactory());
assertThat(compiler.hasStatementsDependentOnNewNodes()).isTrue();
assertThat(compiler.createNodesStatements()).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`DomainObject`:`School`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`Teacher`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
assertThat(compiler.createRelationshipsStatements()).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:`TEACHERS`]->(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:`SCHOOL`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
}
Aggregations