Skip to content

Commit 3ea0214

Browse files
committed
Object-Graph-Mapping with Neo4j-OGM
1 parent a52ba0a commit 3ea0214

11 files changed

Lines changed: 330 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
15+
<attributes>
16+
<attribute name="maven.pomderived" value="true"/>
17+
</attributes>
18+
</classpathentry>
19+
<classpathentry kind="output" path="target/classes"/>
20+
</classpath>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Neo4J OGM HelloWorld</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
21+
<nature>org.eclipse.jdt.core.javanature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
3+
org.eclipse.jdt.core.compiler.compliance=1.5
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.5
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

Neo4j/Neo4J OGM HelloWorld/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.javahelps</groupId>
5+
<artifactId>Neo4JOGMHelloWorld</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<build>
8+
<sourceDirectory>src</sourceDirectory>
9+
<plugins>
10+
<plugin>
11+
<artifactId>maven-compiler-plugin</artifactId>
12+
<version>3.3</version>
13+
<configuration>
14+
<source />
15+
<target />
16+
</configuration>
17+
</plugin>
18+
</plugins>
19+
</build>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.neo4j</groupId>
24+
<artifactId>neo4j-ogm</artifactId>
25+
<version>1.1.3</version>
26+
</dependency>
27+
</dependencies>
28+
</project>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.javahelps.ogmhelloworld;
2+
3+
import java.util.Collections;
4+
5+
import org.neo4j.ogm.session.Session;
6+
import org.neo4j.ogm.session.SessionFactory;
7+
8+
import com.javahelps.ogmhelloworld.entities.Course;
9+
import com.javahelps.ogmhelloworld.entities.Student;
10+
11+
public class Main {
12+
13+
public static final String NEO4J_URL = "http://localhost:7474";
14+
public static final String USERNAME = "neo4j";
15+
public static final String PASSWORD = "admin";
16+
17+
public static void main(String[] args) {
18+
// Create SessionFactory. Pass the package name of the entity classes as
19+
// the argument.
20+
SessionFactory sessionFactory = new SessionFactory("com.javahelps.ogmhelloworld.entities");
21+
22+
// Create the session
23+
Session session = sessionFactory.openSession(NEO4J_URL, USERNAME, PASSWORD);
24+
25+
// Create few courses
26+
Course oop = new Course();
27+
oop.setName("Object Oriented Programming");
28+
oop.setCredits(2.0f);
29+
30+
Course algo = new Course();
31+
algo.setName("Advanced Algorithm");
32+
algo.setCredits(3.0f);
33+
34+
Course db = new Course();
35+
db.setName("Database Internals");
36+
db.setCredits(3.0f);
37+
38+
// Create few students
39+
Student alice = new Student();
40+
alice.setName("Alice");
41+
42+
Student bob = new Student();
43+
bob.setName("Bob");
44+
45+
Student carol = new Student();
46+
carol.setName("Carol");
47+
48+
// Add the courses
49+
alice.getCourses().add(oop);
50+
alice.getCourses().add(algo);
51+
alice.getCourses().add(db);
52+
53+
bob.getCourses().add(oop);
54+
bob.getCourses().add(algo);
55+
56+
carol.getCourses().add(algo);
57+
carol.getCourses().add(db);
58+
59+
// Persist the objects. Persisting students persists courses as well.
60+
session.save(alice);
61+
session.save(bob);
62+
session.save(carol);
63+
64+
// Retrieve Students who enrolled for Advanced Algorithm
65+
Iterable<Student> students = session.query(Student.class,
66+
"MATCH (c:Course)<-[:ENROLLED]-(student) WHERE c.name = 'Advanced Algorithm' RETURN student",
67+
Collections.<String, Object> emptyMap());
68+
69+
// Print all the Students
70+
for (Student stu : students) {
71+
System.out.println(stu.getName());
72+
}
73+
}
74+
75+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.javahelps.ogmhelloworld.entities;
2+
3+
import org.neo4j.ogm.annotation.GraphId;
4+
import org.neo4j.ogm.annotation.NodeEntity;
5+
6+
@NodeEntity
7+
public class Course {
8+
9+
@GraphId
10+
private Long id;
11+
12+
private String name;
13+
14+
private float credits;
15+
16+
public Long getId() {
17+
return id;
18+
}
19+
20+
public void setId(Long id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
public float getCredits() {
33+
return credits;
34+
}
35+
36+
public void setCredits(float credits) {
37+
this.credits = credits;
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
final int prime = 31;
43+
int result = 1;
44+
result = prime * result + ((name == null) ? 0 : name.hashCode());
45+
return result;
46+
}
47+
48+
@Override
49+
public boolean equals(Object obj) {
50+
if (this == obj)
51+
return true;
52+
if (obj == null)
53+
return false;
54+
if (getClass() != obj.getClass())
55+
return false;
56+
Course other = (Course) obj;
57+
if (name == null) {
58+
if (other.name != null)
59+
return false;
60+
} else if (!name.equals(other.name))
61+
return false;
62+
return true;
63+
}
64+
65+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.javahelps.ogmhelloworld.entities;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import org.neo4j.ogm.annotation.GraphId;
7+
import org.neo4j.ogm.annotation.NodeEntity;
8+
import org.neo4j.ogm.annotation.Relationship;
9+
10+
@NodeEntity
11+
public class Student {
12+
13+
@GraphId
14+
private Long id;
15+
16+
private String name;
17+
18+
@Relationship(type = "ENROLLED", direction = Relationship.OUTGOING)
19+
private Set<Course> courses = new HashSet<Course>();
20+
21+
public Long getId() {
22+
return id;
23+
}
24+
25+
public void setId(Long id) {
26+
this.id = id;
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public Set<Course> getCourses() {
38+
return courses;
39+
}
40+
41+
public void setCourses(Set<Course> courses) {
42+
this.courses = courses;
43+
}
44+
45+
@Override
46+
public int hashCode() {
47+
final int prime = 31;
48+
int result = 1;
49+
result = prime * result + ((name == null) ? 0 : name.hashCode());
50+
return result;
51+
}
52+
53+
@Override
54+
public boolean equals(Object obj) {
55+
if (this == obj)
56+
return true;
57+
if (obj == null)
58+
return false;
59+
if (getClass() != obj.getClass())
60+
return false;
61+
Student other = (Student) obj;
62+
if (name == null) {
63+
if (other.name != null)
64+
return false;
65+
} else if (!name.equals(other.name))
66+
return false;
67+
return true;
68+
}
69+
70+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Manifest-Version: 1.0
2+
Built-By: gobinath
3+
Build-Jdk: 1.8.0_60
4+
Created-By: Maven Integration for Eclipse
5+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#Generated by Maven Integration for Eclipse
2+
#Tue Oct 27 20:35:58 IST 2015
3+
version=0.0.1-SNAPSHOT
4+
groupId=com.javahelps
5+
m2e.projectName=Neo4J OGM HelloWorld
6+
m2e.projectLocation=/home/gobinath/Workspace/eclipse/Neo4J OGM HelloWorld
7+
artifactId=Neo4JOGMHelloWorld

0 commit comments

Comments
 (0)