Java Spring Data Neo4j – Code Sample
All of the code shown is available in the spring-data-neo4j-intro-app Github repository.
This application uses a ready-to-use demo database with the movies data set containing Person and Movie entities and the connections between them.

Connect to the database
To connect to Neo4j, you will need credentials and the connection details. Since this sample uses a public database, it will connect without any changes.
Connect to Neo4j – src/main/resources/application.properties
# Neo4j config
spring.neo4j.uri=neo4j+s://demo.neo4jlabs.com
spring.neo4j.authentication.username=movies
spring.neo4j.authentication.password=movies
spring.data.neo4j.database=moviesCode language: Shell Session (shell)
Domain classes
Domain classes represent the data in your domain. In this case, there will be a Movie class and Person class, plus a Role class to map the relationship property between them.
Domain class – domainClasses/Movie.java
@Node
public class Movie {
@Id
@GeneratedValue
private Long id;
private String title;
private int released;
@Property("tagline")
private String description;
@JsonIgnoreProperties("movie")
@Relationship(type = "ACTED_IN", direction = INCOMING)
private List<Role> actors = new ArrayList<>();
@JsonIgnoreProperties({"actedIn", "directed"})
@Relationship(type = "DIRECTED", direction = INCOMING)
private List<Person> directors = new ArrayList<>();
public Movie(String title, int released, String description) {
this.title = title;
this.released = released;
this.description = description;
}
//getters and setters
}Code language: Java (java)
The @JsonIgnoreProperties avoids circular data returning in the output. The rest of the annotations define the entity and properties for mapping in Neo4j. More information is available in the related section of the documentation
Domain class – domainClasses/Person.java
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
@Property("born")
private Integer birthYear;
@JsonIgnoreProperties("person")
@Relationship(type = "ACTED_IN")
private List<Movie> actedIn = new ArrayList<>();
@JsonIgnoreProperties({"actors", "directors"})
@Relationship(type = "DIRECTED")
private List<Movie> directed = new ArrayList<>();
public Person(String name, Integer birthYear) {
this.name = name;
this.birthYear = birthYear;
}
//getters and setters
}Code language: Java (java)
The next class for Role maps the relationship property of the role (or roles) a person played acting in a movie.
Domain class – domainClasses/Role.java
@RelationshipProperties
public class Role {
@Id
@GeneratedValue
private Long id;
private List<String> roles = new ArrayList<>();
@TargetNode
@JsonIgnoreProperties({"actedIn", "directed"})
private Person person;
//getters
}Code language: Java (java)
The @RelationshipProperties defines that this class maps properties on a relationship in Neo4j, and a @TargetNode annotation points to the domain class on the other side of the relationship.
Repository interfaces
Repository interfaces usually contain the methods and queries for retrieving and returning data between the application and the database.
Repository – repositories/MovieRepository.java
public interface MovieRepository extends Neo4jRepository<Movie, Long> {
Movie getMovieByTitle(String title);
Iterable<Movie> findMovieByTitleLike(String title);
}Code language: Java (java)
Two methods retrieve a movie by the title and the movie by a title like the value (handling a fuzzy search). Neither of these require a defined query because Spring can derive some queries by the method name. More information is available in the documentation
Repository – repositories/PersonRepository.java
public interface PersonRepository extends Neo4jRepository<Person, Long> {
Person getPersonByName(String name);
Iterable<Person> findPersonByNameLike(String name);
@Query("MATCH (am:Movie)<-[ai:ACTED_IN]-(p:Person)-[d:DIRECTED]->(dm:Movie) " +
"RETURN p, collect(ai), collect(d), collect(am), collect(dm);")
List<Person> getPersonsWhoActAndDirect();
}Code language: Java (java)
There are two derived methods to find the person by name and a fuzzy search on a name, then there is a method with a defined query using the @Query annotation. The query looks for people who both acted and directed the same movie.
Controller classes
The controller will define the user-facing endpoints for retrieving data. Both person and movie controllers are defined in this application.
MovieController – /controllers/MovieController.java
@RestController
@RequestMapping("/movies")
public class MovieController {
private final MovieRepository movieRepository;
public MovieController(MovieRepository movieRepository) {
this.movieRepository = movieRepository;
}
@GetMapping
public Iterable<Movie> findAll() {
return movieRepository.findAll();
}
@GetMapping("/{title}")
public Movie getMovieByTitle(@PathVariable String title) {
return movieRepository.getMovieByTitle(title);
}
@GetMapping("/search/{title}")
public Iterable<Movie> findMovieByTitleLike(@PathVariable String title) {
return movieRepository.findMovieByTitleLike(title);
}
}Code language: Java (java)
The first two annotations define the class as a rest controller and set the main endpoint (/movies). Each method is begun with a @GetMapping annotation to define it as a GET endpoint. The last two methods use a path variable to pass in the title as a value on the URL path.
PersonController – /controllers/PersonController.java
@RestController
@RequestMapping("/people")
public class PersonController {
private final PersonRepository personRepository;
public PersonController(PersonRepository personRepository) {
this.personRepository = personRepository;
}
@GetMapping
public Iterable<Person> findAllPersons() { return personRepository.findAll(); }
@GetMapping("/{name}")
public Person getPersonByName(@PathVariable String name) {
return personRepository.getPersonByName(name);
}
@GetMapping("/search/{name}")
public Iterable<Person> findPersonByNameLike(@PathVariable String name) {
return personRepository.findPersonByNameLike(name);
}
@GetMapping("/actanddirect")
public List<Person> getPersonsWhoActAndDirect() {
return personRepository.getPersonsWhoActAndDirect();
}
}Code language: Java (java)
The main endpoint for this controller is /people, and the format and syntax of these methods are similar to those seen in the Movie controller.
Run and test the application
You can run the application with the following command at a command line interface or some kind of play button provided in your preferred IDE.
./mvnw spring-boot:runCode language: Shell Session (shell)
Any of the defined endpoints should work, but here are a few to try. Examples for each endpoint are available in theREADME of the Github repository
http ":8080/movies"
http ":8080/people/Carrie Fisher"
http ":8080/people/search/Bale"Code language: Shell Session (shell)
Resources
- Code: Github repository for this code sample
- Documentation: Spring Data Neo4j


