forked from spring-projects/spring-data-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spring-projects#131 - Added Spring Data Neo4j example.
Original pull requests: spring-projects#129, spring-projects#130.
- Loading branch information
Showing
10 changed files
with
425 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>spring-data-neo4j-example</artifactId> | ||
<name>Spring Data Neo4j - Example</name> | ||
<properties> | ||
<spring-data-neo4j.version>4.0.0.BUILD-SNAPSHOT</spring-data-neo4j.version> | ||
<neo4j-ogm.version>1.1.2-SNAPSHOT</neo4j-ogm.version> | ||
<neo4j.version>2.2.4</neo4j.version> | ||
</properties> | ||
|
||
<parent> | ||
<groupId>org.springframework.data.examples</groupId> | ||
<artifactId>spring-data-neo4j-examples</artifactId> | ||
<version>1.0.0.BUILD-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.data</groupId> | ||
<artifactId>spring-data-neo4j</artifactId> | ||
<version>${spring-data-neo4j.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.data</groupId> | ||
<artifactId>spring-data-neo4j</artifactId> | ||
<version>${spring-data-neo4j.version}</version> | ||
<type>test-jar</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.neo4j</groupId> | ||
<artifactId>neo4j-ogm</artifactId> | ||
<version>${neo4j-ogm.version}</version> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.neo4j.app</groupId> | ||
<artifactId>neo4j-server</artifactId> | ||
<version>${neo4j.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.neo4j.test</groupId> | ||
<artifactId>neo4j-harness</artifactId> | ||
<version>${neo4j.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<repositories> | ||
<repository> | ||
<id>spring-libs-snapshot</id> | ||
<name>Spring</name> | ||
<url>http://repo.spring.io/libs-snapshot</url> | ||
</repository> | ||
</repositories> | ||
</project> |
68 changes: 68 additions & 0 deletions
68
neo4j/example/src/main/java/example/springdata/neo4j/domain/Actor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package example.springdata.neo4j.domain; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.neo4j.ogm.annotation.GraphId; | ||
import org.neo4j.ogm.annotation.NodeEntity; | ||
import org.neo4j.ogm.annotation.Relationship; | ||
|
||
/** | ||
* An Actor node entity. | ||
* | ||
* @author Luanne Misquitta | ||
*/ | ||
@NodeEntity(label = "Actor") | ||
public class Actor { | ||
|
||
@GraphId private Long id; | ||
private String name; | ||
|
||
@Relationship(type = "ACTED_IN") | ||
private Set<Role> roles = new HashSet<>(); | ||
|
||
public Actor() { | ||
} | ||
|
||
public Actor(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public Set<Role> getRoles() { | ||
return roles; | ||
} | ||
|
||
public void actedIn(Movie movie, String roleName) { | ||
|
||
Role role = new Role(); | ||
role.setRole(roleName); | ||
role.setActor(this); | ||
role.setMovie(movie); | ||
roles.add(role); | ||
movie.getRoles().add(role); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
neo4j/example/src/main/java/example/springdata/neo4j/domain/Movie.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package example.springdata.neo4j.domain; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.neo4j.ogm.annotation.GraphId; | ||
import org.neo4j.ogm.annotation.NodeEntity; | ||
import org.neo4j.ogm.annotation.Relationship; | ||
|
||
/** | ||
* A Movie node entity. | ||
* | ||
* @author Luanne Misquitta | ||
*/ | ||
@NodeEntity(label = "Movie") | ||
public class Movie { | ||
|
||
@GraphId private Long id; | ||
private String title; | ||
|
||
@Relationship(type = "ACTED_IN", direction = "INCOMING") | ||
private Set<Role> roles = new HashSet<>(); | ||
|
||
public Movie() { | ||
} | ||
|
||
public Movie(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
@Relationship(type = "ACTED_IN", direction = "INCOMING") | ||
public Set<Role> getRoles() { | ||
return roles; | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
neo4j/example/src/main/java/example/springdata/neo4j/domain/Role.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package example.springdata.neo4j.domain; | ||
|
||
import org.neo4j.ogm.annotation.EndNode; | ||
import org.neo4j.ogm.annotation.GraphId; | ||
import org.neo4j.ogm.annotation.RelationshipEntity; | ||
import org.neo4j.ogm.annotation.StartNode; | ||
|
||
/** | ||
* A Role relationship entity between an actor and movie. | ||
* @author Luanne Misquitta | ||
*/ | ||
@RelationshipEntity(type = "ACTED_IN") | ||
public class Role { | ||
|
||
@GraphId private Long id; | ||
@StartNode private Actor actor; | ||
@EndNode private Movie movie; | ||
private String role; | ||
|
||
public Role() { | ||
} | ||
|
||
public Actor getActor() { | ||
return actor; | ||
} | ||
|
||
public void setActor(Actor actor) { | ||
this.actor = actor; | ||
} | ||
|
||
public Movie getMovie() { | ||
return movie; | ||
} | ||
|
||
public void setMovie(Movie movie) { | ||
this.movie = movie; | ||
} | ||
|
||
public String getRole() { | ||
return role; | ||
} | ||
|
||
public void setRole(String role) { | ||
this.role = role; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
neo4j/example/src/main/java/example/springdata/neo4j/repo/ActorRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package example.springdata.neo4j.repo; | ||
|
||
import example.springdata.neo4j.domain.Actor; | ||
import org.springframework.data.neo4j.repository.GraphRepository; | ||
|
||
/** | ||
* GraphRepository for Actors. | ||
* @author Luanne Misquitta | ||
*/ | ||
public interface ActorRepository extends GraphRepository<Actor> { | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
neo4j/example/src/test/java/example/springdata/neo4j/ActorRepositoryIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package example.springdata.neo4j; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import example.springdata.neo4j.domain.Actor; | ||
import example.springdata.neo4j.domain.Movie; | ||
import example.springdata.neo4j.domain.Role; | ||
import example.springdata.neo4j.repo.ActorRepository; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
/** | ||
* Simple integration test demonstrating the use of the ActorRepository | ||
* @author Luanne Misquitta | ||
*/ | ||
@ContextConfiguration(classes = {ExampleConfig.class}) | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) | ||
public class ActorRepositoryIntegrationTest { | ||
|
||
@Autowired ActorRepository actorRepository; | ||
|
||
/** | ||
* @see DATAGRAPH-752 | ||
* Test case to demonstrate saving node and relationship entities, and loading them via the repository. | ||
*/ | ||
@Test | ||
public void shouldBeAbleToSaveAndLoadActor() { | ||
|
||
Actor daniel = new Actor("Daniel Radcliffe"); | ||
Movie goblet = new Movie("Harry Potter and the Goblet of Fire"); | ||
daniel.actedIn(goblet,"Harry Potter"); | ||
actorRepository.save(daniel); //saves the actor and the movie | ||
|
||
|
||
Actor actor = actorRepository.findOne(daniel.getId()); | ||
assertNotNull(actor); | ||
assertEquals(daniel.getName(),actor.getName()); | ||
assertEquals(1,actor.getRoles().size()); | ||
Role role = actor.getRoles().iterator().next(); | ||
assertEquals("Harry Potter", role.getRole()); | ||
} | ||
|
||
} |
Oops, something went wrong.