Skip to content

Commit f90bed3

Browse files
luanneodrotbohm
authored andcommitted
spring-projects#131 - Added Spring Data Neo4j example.
Original pull requests: spring-projects#129, spring-projects#130.
1 parent 0c1bee0 commit f90bed3

File tree

10 files changed

+425
-0
lines changed

10 files changed

+425
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ We have separate folders for the samples of individual modules:
3939

4040
* `example` - Example how to use basic text search, geo-spatial search and facets.
4141

42+
## Spring Data Neo4j
43+
44+
* `example` - Example to show basic node and relationship entities and repository usage.
45+
4246
## Spring Data web support
4347

4448
* `web` - Example for Spring Data web integration (binding `Pageable` instances to Spring MVC controller methods, using interfaces to bind Spring MVCrequest payloads).

neo4j/example/pom.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
4+
<project xmlns="http://maven.apache.org/POM/4.0.0"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
7+
<modelVersion>4.0.0</modelVersion>
8+
9+
<artifactId>spring-data-neo4j-example</artifactId>
10+
<name>Spring Data Neo4j - Example</name>
11+
<properties>
12+
<spring-data-neo4j.version>4.0.0.BUILD-SNAPSHOT</spring-data-neo4j.version>
13+
<neo4j-ogm.version>1.1.2-SNAPSHOT</neo4j-ogm.version>
14+
<neo4j.version>2.2.4</neo4j.version>
15+
</properties>
16+
17+
<parent>
18+
<groupId>org.springframework.data.examples</groupId>
19+
<artifactId>spring-data-neo4j-examples</artifactId>
20+
<version>1.0.0.BUILD-SNAPSHOT</version>
21+
<relativePath>../pom.xml</relativePath>
22+
</parent>
23+
24+
<dependencies>
25+
26+
<dependency>
27+
<groupId>org.springframework.data</groupId>
28+
<artifactId>spring-data-neo4j</artifactId>
29+
<version>${spring-data-neo4j.version}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.data</groupId>
34+
<artifactId>spring-data-neo4j</artifactId>
35+
<version>${spring-data-neo4j.version}</version>
36+
<type>test-jar</type>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.neo4j</groupId>
40+
<artifactId>neo4j-ogm</artifactId>
41+
<version>${neo4j-ogm.version}</version>
42+
<type>test-jar</type>
43+
<scope>test</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.neo4j.app</groupId>
47+
<artifactId>neo4j-server</artifactId>
48+
<version>${neo4j.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>junit</groupId>
52+
<artifactId>junit</artifactId>
53+
<version>4.12</version>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.neo4j.test</groupId>
58+
<artifactId>neo4j-harness</artifactId>
59+
<version>${neo4j.version}</version>
60+
<scope>test</scope>
61+
</dependency>
62+
</dependencies>
63+
64+
<repositories>
65+
<repository>
66+
<id>spring-libs-snapshot</id>
67+
<name>Spring</name>
68+
<url>http://repo.spring.io/libs-snapshot</url>
69+
</repository>
70+
</repositories>
71+
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package example.springdata.neo4j.domain;
18+
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
22+
import org.neo4j.ogm.annotation.GraphId;
23+
import org.neo4j.ogm.annotation.NodeEntity;
24+
import org.neo4j.ogm.annotation.Relationship;
25+
26+
/**
27+
* An Actor node entity.
28+
*
29+
* @author Luanne Misquitta
30+
*/
31+
@NodeEntity(label = "Actor")
32+
public class Actor {
33+
34+
@GraphId private Long id;
35+
private String name;
36+
37+
@Relationship(type = "ACTED_IN")
38+
private Set<Role> roles = new HashSet<>();
39+
40+
public Actor() {
41+
}
42+
43+
public Actor(String name) {
44+
this.name = name;
45+
}
46+
47+
public String getName() {
48+
return name;
49+
}
50+
51+
public Long getId() {
52+
return id;
53+
}
54+
55+
public Set<Role> getRoles() {
56+
return roles;
57+
}
58+
59+
public void actedIn(Movie movie, String roleName) {
60+
61+
Role role = new Role();
62+
role.setRole(roleName);
63+
role.setActor(this);
64+
role.setMovie(movie);
65+
roles.add(role);
66+
movie.getRoles().add(role);
67+
}
68+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package example.springdata.neo4j.domain;
18+
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
22+
import org.neo4j.ogm.annotation.GraphId;
23+
import org.neo4j.ogm.annotation.NodeEntity;
24+
import org.neo4j.ogm.annotation.Relationship;
25+
26+
/**
27+
* A Movie node entity.
28+
*
29+
* @author Luanne Misquitta
30+
*/
31+
@NodeEntity(label = "Movie")
32+
public class Movie {
33+
34+
@GraphId private Long id;
35+
private String title;
36+
37+
@Relationship(type = "ACTED_IN", direction = "INCOMING")
38+
private Set<Role> roles = new HashSet<>();
39+
40+
public Movie() {
41+
}
42+
43+
public Movie(String title) {
44+
this.title = title;
45+
}
46+
47+
public String getTitle() {
48+
return title;
49+
}
50+
51+
public void setTitle(String title) {
52+
this.title = title;
53+
}
54+
55+
@Relationship(type = "ACTED_IN", direction = "INCOMING")
56+
public Set<Role> getRoles() {
57+
return roles;
58+
}
59+
60+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.neo4j.domain;
17+
18+
import org.neo4j.ogm.annotation.EndNode;
19+
import org.neo4j.ogm.annotation.GraphId;
20+
import org.neo4j.ogm.annotation.RelationshipEntity;
21+
import org.neo4j.ogm.annotation.StartNode;
22+
23+
/**
24+
* A Role relationship entity between an actor and movie.
25+
* @author Luanne Misquitta
26+
*/
27+
@RelationshipEntity(type = "ACTED_IN")
28+
public class Role {
29+
30+
@GraphId private Long id;
31+
@StartNode private Actor actor;
32+
@EndNode private Movie movie;
33+
private String role;
34+
35+
public Role() {
36+
}
37+
38+
public Actor getActor() {
39+
return actor;
40+
}
41+
42+
public void setActor(Actor actor) {
43+
this.actor = actor;
44+
}
45+
46+
public Movie getMovie() {
47+
return movie;
48+
}
49+
50+
public void setMovie(Movie movie) {
51+
this.movie = movie;
52+
}
53+
54+
public String getRole() {
55+
return role;
56+
}
57+
58+
public void setRole(String role) {
59+
this.role = role;
60+
}
61+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.neo4j.repo;
17+
18+
import example.springdata.neo4j.domain.Actor;
19+
import org.springframework.data.neo4j.repository.GraphRepository;
20+
21+
/**
22+
* GraphRepository for Actors.
23+
* @author Luanne Misquitta
24+
*/
25+
public interface ActorRepository extends GraphRepository<Actor> {
26+
27+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package example.springdata.neo4j;
18+
19+
import static org.junit.Assert.*;
20+
21+
import example.springdata.neo4j.domain.Actor;
22+
import example.springdata.neo4j.domain.Movie;
23+
import example.springdata.neo4j.domain.Role;
24+
import example.springdata.neo4j.repo.ActorRepository;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.test.annotation.DirtiesContext;
29+
import org.springframework.test.context.ContextConfiguration;
30+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31+
32+
/**
33+
* Simple integration test demonstrating the use of the ActorRepository
34+
* @author Luanne Misquitta
35+
*/
36+
@ContextConfiguration(classes = {ExampleConfig.class})
37+
@RunWith(SpringJUnit4ClassRunner.class)
38+
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
39+
public class ActorRepositoryIntegrationTest {
40+
41+
@Autowired ActorRepository actorRepository;
42+
43+
/**
44+
* @see DATAGRAPH-752
45+
* Test case to demonstrate saving node and relationship entities, and loading them via the repository.
46+
*/
47+
@Test
48+
public void shouldBeAbleToSaveAndLoadActor() {
49+
50+
Actor daniel = new Actor("Daniel Radcliffe");
51+
Movie goblet = new Movie("Harry Potter and the Goblet of Fire");
52+
daniel.actedIn(goblet,"Harry Potter");
53+
actorRepository.save(daniel); //saves the actor and the movie
54+
55+
56+
Actor actor = actorRepository.findOne(daniel.getId());
57+
assertNotNull(actor);
58+
assertEquals(daniel.getName(),actor.getName());
59+
assertEquals(1,actor.getRoles().size());
60+
Role role = actor.getRoles().iterator().next();
61+
assertEquals("Harry Potter", role.getRole());
62+
}
63+
64+
}

0 commit comments

Comments
 (0)