Search in sources :

Example 6 with School

use of org.neo4j.ogm.domain.education.School in project neo4j-ogm by neo4j.

the class EntityGraphMapperTest method testVariablePersistenceToDepthZero.

@Test
public void testVariablePersistenceToDepthZero() {
    Teacher claraOswald = new Teacher();
    Teacher dannyPink = new Teacher();
    School coalHillSchool = new School("Coal Hill");
    coalHillSchool.setTeachers(Arrays.asList(claraOswald, dannyPink));
    session.save(coalHillSchool, 0);
    // we don't expect the teachers to be persisted when persisting the school to depth 0
    session.clear();
    assertThat(session.query("MATCH (s:School:DomainObject {name:'Coal Hill'}) RETURN s", emptyMap()).queryResults()).hasSize(1);
    assertThat(session.query("MATCH (t:Teacher) RETURN t", emptyMap()).queryResults()).hasSize(0);
}
Also used : School(org.neo4j.ogm.domain.education.School) Teacher(org.neo4j.ogm.domain.education.Teacher) Test(org.junit.Test)

Example 7 with School

use of org.neo4j.ogm.domain.education.School 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);
}
Also used : School(org.neo4j.ogm.domain.education.School) Teacher(org.neo4j.ogm.domain.education.Teacher) Test(org.junit.Test)

Example 8 with School

use of org.neo4j.ogm.domain.education.School in project neo4j-ogm by neo4j.

the class LoadCapabilityTest method loadAllByInstancesShouldLoadAllClasses.

@Test
public void loadAllByInstancesShouldLoadAllClasses() {
    SessionFactory sf = new SessionFactory(getDriver(), "org.neo4j.ogm.domain.education");
    Session sessionWithEducationDomain = sf.openSession();
    School school = new School();
    Student student = new Student();
    sessionWithEducationDomain.save(school);
    sessionWithEducationDomain.save(student);
    Collection<DomainObject> loaded = sessionWithEducationDomain.loadAll(Arrays.asList(school, student));
    assertThat(loaded).contains(school, student);
}
Also used : SessionFactory(org.neo4j.ogm.session.SessionFactory) School(org.neo4j.ogm.domain.education.School) DomainObject(org.neo4j.ogm.domain.education.DomainObject) Student(org.neo4j.ogm.domain.education.Student) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Session(org.neo4j.ogm.session.Session) Test(org.junit.Test)

Example 9 with School

use of org.neo4j.ogm.domain.education.School 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");
}
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) Test(org.junit.Test)

Example 10 with School

use of org.neo4j.ogm.domain.education.School 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");
}
Also used : School(org.neo4j.ogm.domain.education.School) Teacher(org.neo4j.ogm.domain.education.Teacher) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)13 School (org.neo4j.ogm.domain.education.School)13 Teacher (org.neo4j.ogm.domain.education.Teacher)12 Course (org.neo4j.ogm.domain.education.Course)4 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)3 List (java.util.List)2 Student (org.neo4j.ogm.domain.education.Student)2 Statement (org.neo4j.ogm.request.Statement)2 Session (org.neo4j.ogm.session.Session)2 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DomainObject (org.neo4j.ogm.domain.education.DomainObject)1 Result (org.neo4j.ogm.model.Result)1 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)1 SessionFactory (org.neo4j.ogm.session.SessionFactory)1