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);
}
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();
}
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.'");
}
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);
}
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);
}
Aggregations