Search in sources :

Example 11 with Teacher

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

the class EducationIntegrationTest method loadingASchoolWithNegativeDepthShouldLoadAllConnectedEntities.

/**
 * @see DATAGRAPH-595
 */
@Test
public void loadingASchoolWithNegativeDepthShouldLoadAllConnectedEntities() {
    // Create students, teachers, courses and a school
    School hogwarts = new School("Hogwarts");
    Student harry = new Student("Harry Potter");
    Student ron = new Student("Ron Weasley");
    Student hermione = new Student("Hermione Granger");
    Course transfiguration = new Course("Transfiguration");
    transfiguration.setStudents(Arrays.asList(harry, hermione, ron));
    Course potions = new Course("Potions");
    potions.setStudents(Arrays.asList(ron, hermione));
    Course dark = new Course("Defence Against The Dark Arts");
    dark.setStudents(Collections.singletonList(harry));
    Teacher minerva = new Teacher("Minerva McGonagall");
    minerva.setCourses(Collections.singletonList(transfiguration));
    minerva.setSchool(hogwarts);
    Teacher severus = new Teacher("Severus Snape");
    severus.setCourses(Arrays.asList(potions, dark));
    severus.setSchool(hogwarts);
    hogwarts.setTeachers(Arrays.asList(minerva, severus));
    session.save(hogwarts);
    session.clear();
    // Load the school with depth -1
    hogwarts = session.load(School.class, hogwarts.getId(), -1);
    assertThat(hogwarts.getTeachers()).hasSize(2);
    for (Teacher teacher : hogwarts.getTeachers()) {
        if (teacher.getName().equals("Severus Snape")) {
            assertThat(teacher.getCourses()).hasSize(2);
            for (Course course : teacher.getCourses()) {
                if (course.getName().equals("Potions")) {
                    assertThat(course.getStudents()).hasSize(2);
                } else {
                    assertThat(course.getStudents()).hasSize(1);
                }
            }
        } else {
            assertThat(teacher.getCourses()).hasSize(1);
            assertThat(teacher.getCourses().get(0).getStudents()).hasSize(3);
        }
    }
}
Also used : School(org.neo4j.ogm.domain.education.School) Teacher(org.neo4j.ogm.domain.education.Teacher) Student(org.neo4j.ogm.domain.education.Student) Course(org.neo4j.ogm.domain.education.Course) Test(org.junit.Test)

Example 12 with Teacher

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

the class EducationTest method testFetchCoursesTaughtByAllTeachers.

@Test
public void testFetchCoursesTaughtByAllTeachers() throws Exception {
    // note: idempotent!
    Map<String, Teacher> teachers = loadTeachers();
    // this response is for an imagined request: "match p = (c:COURSE)--(o) where id(c) in [....] RETURN p"
    // i.e. we have a set of partially loaded courses attached to our teachers which we now want to
    // hydrate by getting all their relationships
    hydrateCourses(teachers.values());
    Set<Course> courses = new HashSet<>();
    for (Teacher teacher : teachers.values()) {
        for (Course course : teacher.getCourses()) {
            if (!courses.contains(course)) {
                List<Student> students = course.getStudents();
                switch(course.getName()) {
                    case "Maths":
                        checkMaths(students);
                        break;
                    case "Physics":
                        checkPhysics(students);
                        break;
                    case "Philosophy and Ethics":
                        checkPhilosophyAndEthics(students);
                        break;
                    case "PE":
                        checkPE(students);
                        break;
                    case "History":
                        checkHistory(students);
                        break;
                    case "Geography":
                        checkGeography(students);
                        break;
                    default:
                        checkEnglish(students);
                        break;
                }
                courses.add(course);
            }
        }
    }
    assertThat(courses).hasSize(7);
}
Also used : Teacher(org.neo4j.ogm.domain.education.Teacher) Course(org.neo4j.ogm.domain.education.Course) Student(org.neo4j.ogm.domain.education.Student) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with Teacher

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

the class EducationTest method loadTeachers.

private Map<String, Teacher> loadTeachers() {
    Map<String, Teacher> teachers = new HashMap<>();
    Collection<Teacher> teacherList = session.loadAll(Teacher.class);
    for (Teacher teacher : teacherList) {
        teachers.put(teacher.getName(), teacher);
    }
    return teachers;
}
Also used : HashMap(java.util.HashMap) Teacher(org.neo4j.ogm.domain.education.Teacher)

Example 14 with Teacher

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

the class EntityGraphMapperTest method persistManyToOneObjectFromSingletonSide.

@Test
public void persistManyToOneObjectFromSingletonSide() {
    Iterable<Map<String, Object>> executionResult = session.query("CREATE (s:School:DomainObject {name:'Waller'})-[:TEACHERS]->(t:Teacher {name:'Mary'})-[:SCHOOL]->(s) " + "RETURN id(s) AS school_id, id(t) AS teacher_id", emptyMap()).queryResults();
    Map<String, Object> resultSetRow = executionResult.iterator().next();
    Long wallerId = Long.valueOf(resultSetRow.get("school_id").toString());
    Long maryId = Long.valueOf(resultSetRow.get("teacher_id").toString());
    session.clear();
    School waller = session.load(School.class, wallerId);
    Teacher mary = session.load(Teacher.class, maryId);
    mary.setId(maryId);
    // create a new teacher and add him to the school
    Teacher jim = new Teacher("Jim");
    jim.setSchool(waller);
    // ensure that the domain objects are mutually established by the code
    assertThat(waller.getTeachers().contains(jim)).isTrue();
    session.save(jim);
    session.clear();
    assertThat(session.query("MATCH " + "(s:School:DomainObject {name:'Waller'}), " + "(m:Teacher {name:'Mary'}), " + "(j:Teacher {name:'Jim'}) " + "WHERE (j)-[:SCHOOL]->(s) and " + "(m)-[:SCHOOL]->(s) and " + "(s)-[:TEACHERS]->(j) and " + "(s)-[:TEACHERS]->(m) return s, m, j", emptyMap()).queryResults()).hasSize(1);
}
Also used : School(org.neo4j.ogm.domain.education.School) Teacher(org.neo4j.ogm.domain.education.Teacher) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 15 with Teacher

use of org.neo4j.ogm.domain.education.Teacher 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)

Aggregations

Teacher (org.neo4j.ogm.domain.education.Teacher)25 Test (org.junit.Test)24 School (org.neo4j.ogm.domain.education.School)12 Course (org.neo4j.ogm.domain.education.Course)9 List (java.util.List)6 Student (org.neo4j.ogm.domain.education.Student)6 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)4 Result (org.neo4j.ogm.model.Result)4 Statement (org.neo4j.ogm.request.Statement)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Session (org.neo4j.ogm.session.Session)3 Map (java.util.Map)2 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)2 HashSet (java.util.HashSet)1 TeachesAt (org.neo4j.ogm.domain.education.TeachesAt)1