Search in sources :

Example 1 with Student

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

the class EntityGraphMapperTest method shouldCorrectlyRemoveRelationshipWhenItemIsRemovedFromCollection.

@Test
public void shouldCorrectlyRemoveRelationshipWhenItemIsRemovedFromCollection() {
    // simple music course with three students
    Iterable<Map<String, Object>> executionResult = session.query("CREATE (c:Course {name:'GCSE Music'}), " + "(c)-[:STUDENTS]->(x:Student:DomainObject {name:'Xavier'}), " + "(c)-[:STUDENTS]->(y:Student:DomainObject {name:'Yvonne'}), " + "(c)-[:STUDENTS]->(z:Student:DomainObject {name:'Zack'}) " + "RETURN id(c) AS course_id, id(x) AS xid, id(y) AS yid, id(z) AS zid", emptyMap()).queryResults();
    Map<String, ?> results = executionResult.iterator().next();
    session.clear();
    Long mid = (Long) results.get("course_id");
    Long xid = (Long) results.get("xid");
    Long yid = (Long) results.get("yid");
    Long zid = (Long) results.get("zid");
    Course music = session.load(Course.class, mid);
    Student xavier = session.load(Student.class, xid);
    Student yvonne = session.load(Student.class, yid);
    Student zack = session.load(Student.class, zid);
    music.setStudents(Arrays.asList(yvonne, xavier, zack));
    // object is now "loaded"
    // now, update the domain model, setting yvonne as the only music student (i.e remove zack and xavier)
    music.setStudents(Arrays.asList(yvonne));
    session.save(music);
    session.clear();
    assertThat(session.query("MATCH (a:Student:DomainObject {name:'Xavier'}), " + "(b:Student:DomainObject {name:'Zack'}), " + "(c:Course {name:'GCSE Music'})-[:STUDENTS]->(:Student:DomainObject {name:'Yvonne'}) return a,b,c", emptyMap()).queryResults()).hasSize(1);
}
Also used : Course(org.neo4j.ogm.domain.education.Course) Student(org.neo4j.ogm.domain.education.Student) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with Student

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

the class EntityGraphMapperTest method doNothingIfNothingHasChanged.

@Test
public void doNothingIfNothingHasChanged() {
    Long existingNodeId = (Long) session.query("CREATE (s:Student:DomainObject {name:'Sheila Smythe'}) RETURN id(s) AS id", emptyMap()).queryResults().iterator().next().get("id");
    Student sheila = new Student();
    sheila.setId(existingNodeId);
    sheila.setName("Sheila Smythe");
    mappingContext.addNodeEntity(sheila);
    session.clear();
    Compiler compiler = this.mapper.map(sheila).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    Statements cypher = new Statements(compiler.getAllStatements());
    assertThat(cypher.getStatements()).isEmpty();
}
Also used : Compiler(org.neo4j.ogm.cypher.compiler.Compiler) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) Statements(org.neo4j.ogm.request.Statements) Student(org.neo4j.ogm.domain.education.Student) Test(org.junit.Test)

Example 3 with Student

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

the class EntityGraphMapperTest method shouldThrowInvalidRelationshipTargetExceptionOnNullElements.

// GH-781
@Test
public void shouldThrowInvalidRelationshipTargetExceptionOnNullElements() {
    Course course = new Course("Some course");
    Student student1 = new Student("A student");
    Student student2 = new Student("Another student");
    course.setStudents(Arrays.asList(student1, null, student2));
    assertThatExceptionOfType(InvalidRelationshipTargetException.class).isThrownBy(() -> session.save(course)).withMessage("The relationship 'STUDENTS' from 'org.neo4j.ogm.domain.education.Course' to 'org.neo4j.ogm.domain.education.Student' stored on '#students' contains 'null', which is an invalid target for this relationship.'");
}
Also used : Course(org.neo4j.ogm.domain.education.Course) Student(org.neo4j.ogm.domain.education.Student) Test(org.junit.Test)

Example 4 with Student

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

the class EntityGraphMapperTest method shouldCorrectlyPersistObjectGraphsSeveralLevelsDeep.

@Test
public void shouldCorrectlyPersistObjectGraphsSeveralLevelsDeep() {
    Student sheila = new Student();
    sheila.setName("Sheila Smythe");
    Student gary = new Student();
    gary.setName("Gary Jones");
    Student winston = new Student();
    winston.setName("Winston Charles");
    Course physics = new Course();
    physics.setName("GCSE Physics");
    physics.setStudents(Arrays.asList(gary, sheila));
    Course maths = new Course();
    maths.setName("A-Level Mathematics");
    maths.setStudents(Arrays.asList(sheila, winston));
    Teacher teacher = new Teacher();
    teacher.setName("Mrs Kapoor");
    teacher.setCourses(Arrays.asList(physics, maths));
    session.save(teacher);
    session.clear();
    assertThat(session.query("MATCH (t:Teacher {name:'Mrs Kapoor'}), " + "(p:Course {name:'GCSE Physics'}), (m:Course {name:'A-Level Mathematics'}), " + "(s:Student:DomainObject {name:'Sheila Smythe'}), " + "(g:Student:DomainObject {name:'Gary Jones'}), " + "(w:Student:DomainObject {name:'Winston Charles'}) " + "WHERE (t)-[:COURSES]->(p)-[:STUDENTS]->(s) and (t)-[:COURSES]->(m)-[:STUDENTS]->(s) and " + "(p)-[:STUDENTS]->(g) and (m)-[:STUDENTS]->(w) return t, p, m, s, g, w", emptyMap()).queryResults()).hasSize(1);
}
Also used : 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 5 with Student

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

the class EducationIntegrationTest method loadingCourseByPropertyShouldNotLoadOtherEntitiesWithSamePropertyValue.

@Test
public void loadingCourseByPropertyShouldNotLoadOtherEntitiesWithSamePropertyValue() {
    // create a course
    Course course = new Course("CompSci");
    // create a student with the same name as the course
    Student student = new Student("CompSci");
    // relate them so they're both in the mappingContext
    course.setStudents(Collections.singletonList(student));
    session.save(course);
    // fetch Courses by name
    Collection<Course> courses = session.loadAll(Course.class, new Filter("name", ComparisonOperator.EQUALS, "CompSci"));
    assertThat(courses).hasSize(1);
    assertThat(courses.iterator().next()).isEqualTo(course);
    assertThat(courses.iterator().next().getStudents()).hasSize(1);
}
Also used : Filter(org.neo4j.ogm.cypher.Filter) Course(org.neo4j.ogm.domain.education.Course) Student(org.neo4j.ogm.domain.education.Student) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)18 Student (org.neo4j.ogm.domain.education.Student)18 Course (org.neo4j.ogm.domain.education.Course)11 Teacher (org.neo4j.ogm.domain.education.Teacher)6 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Statement (org.neo4j.ogm.request.Statement)3 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)2 School (org.neo4j.ogm.domain.education.School)2 Session (org.neo4j.ogm.session.Session)2 SessionFactory (org.neo4j.ogm.session.SessionFactory)2 HashSet (java.util.HashSet)1 Filter (org.neo4j.ogm.cypher.Filter)1 Compiler (org.neo4j.ogm.cypher.compiler.Compiler)1 DomainObject (org.neo4j.ogm.domain.education.DomainObject)1 Statements (org.neo4j.ogm.request.Statements)1 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)1