Spring Data Neo4j was the first Spring Data project, started by the CEOs Rod Johnson and Emil Eifrem. It enables convenient integration of Neo4j in your Spring-based application. It provides object-graph mapping functionality and other features common to the Spring Data projects.
Note
|
This project uses Spring Data Neo4j 4 which is a complete rewrite from earlier versions. It is optimized for working with Neo4j Server and based on Neo4j’s query language, Cypher. |
The example project is described in detail on the Neo4j Developer Site
-
open the web-interface at http://localhost:7474
-
configure "movies" as password
-
run
:play movies
command, and click and run the Cypher statement to insert the dataset -
clone this project from GitHub
-
run the project with
mvn spring-boot:run
.
To use Neo4j with Spring Data Neo4j, you just add the dependency for Spring-Boot and Spring-Data-Neo4j to your build setup.
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
After setting up a Java-based Spring-Boot configuration,
@EnableTransactionManagement
@Import(RepositoryRestMvcConfiguration.class)
@EnableScheduling
@EnableAutoConfiguration
@ComponentScan(basePackages = {"movies.spring.data.neo4j.services"})
@Configuration
@EnableNeo4jRepositories(basePackages = "movies.spring.data.neo4j.repositories")
public class MyNeo4jConfiguration extends Neo4jConfiguration {
public static final String URL = System.getenv("NEO4J_URL") != null ? System.getenv("NEO4J_URL") : "http://localhost:7474";
@Bean
public org.neo4j.ogm.config.Configuration getConfiguration() {
org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
config
.driverConfiguration()
.setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
.setURI(URL);
return config;
}
@Override
public SessionFactory getSessionFactory() {
return new SessionFactory("movies.spring.data.neo4j.domain");
}
}
and annotating your @Node-
and @RelationshipEntity
, you can use the Neo4jTemplate
to access Neo4j APIs and object graph mapping functionality.
@NodeEntity
public class Movie {
@GraphId Long id;
String title;
int released;
String tagline;
@Relationship(type="ACTED_IN", direction = Relationship.INCOMING) List<Role> roles;
...
}
Additionally you can leverage the convenient Spring-Data repositories to get interface-based DAO implementations injected into your Spring Components.
@RepositoryRestResource(collectionResourceRel = "movies", path = "movies")
public interface MovieRepository extends GraphRepository<Movie> {
Movie findByTitle(@Param("title") String title);
@Query("MATCH (m:Movie) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m")
Collection<Movie> findByTitleContaining(@Param("title") String title);
@Query("MATCH (m:Movie)<-[:ACTED_IN]-(a:Person) RETURN m.title as movie, collect(a.name) as cast LIMIT {limit}")
List<Map<String,Object>> graph(@Param("limit") int limit);
}
In our case we use the repository from a MovieService
to compute the graph representation for the visualization.
The service is then injected into our main Boot application, which also doubles as @RestMvcController
which exposes the /graph
endpoint.
The other two endpoints for finding multiple movies by title and loading a single movie are provided out of the box by the Spring-Data-Rest project which exposes our MovieRepository
as REST endpoints.
The rendering of the movie objects (and related entities) happens automatically out of the box via Jackson mapping.
To avoid circular JSON generation, the JSOGGenerator
is used as id-generator.
See the {github}/examples/java/spring-data-neo4j[GitHub repository] for the source code.
These are the components of our Web Application:
-
Application Type: Spring-Boot Java Web Application (Jetty)
-
Web framework: Spring-Boot enabled Spring-WebMVC, Spring-Data-Rest
-
Persistence Access: Spring-Data-Neo4j 4.1.1
-
Database: Neo4j-Server
-
Frontend: jquery, bootstrap, d3.js
Get Movie
// JSON object for single movie with cast curl http://localhost:8080/movies/search/findByTitle?title=The%20Matrix // list of JSON objects for movie search results curl http://localhost:8080/movies/search/findByTitleContaining?title=matrix // JSON object for whole graph viz (nodes, links - arrays) curl http://localhost:8080/graph