Goals If you are a Java developer, this guide provides an overview of options for connecting to Neo4j. While this guide is not comprehensive, it will introduce the different APIs and link to the relevant resources. Prerequisites You should be… Learn More →
If you are a Java developer, this guide provides an overview of options for connecting to Neo4j. While this guide is not comprehensive, it will introduce the different APIs and link to the relevant resources.
You should be familiar with the graph database concepts and the property graph model. Having installed Neo4j and tried out our Cypher Query language helps too. When developing with Neo4j, please use JDK 7 or 8 and your favorite IDE.
- Neo4j for Java Developers
- The Example Project
- Neo4j Java Driver
- Spring Data Neo4j
- Object-Graph-Mapping with Neo4j-OGM
- Using Neo4j’s Embedded Java API
- User Defined Procedures
- Extending Neo4j Server with a REST Server Extension
- Using Neo4j Server with JDBC
- Neo4j Community Drivers
- Groovy & Grails: Neo4j Grails Plugin
- Clojure: Neocons
- Scala: AnormCypher
- JPA: Hibernate OGM
Neo4j for Java Developers
Neo4j offers a rich set of integration possibilities for developers using Java or other JVM-languages.
The standalone Neo4j Server can be installed on any machine and then accessed via its binary “Bolt” protocol through our official driver or any bolt-enabled drivers.
The server’s additional HTTP API is used by many other integrations, using drivers or any other HTTP client library.
You can also extend Neo4j by implementing user defined procedures for Cypher in Java or other JVM languages, writing server extensions which act as REST-endpoints or by embedding Neo4j into your own Java applications.
|Please note that starting with version 3.0 you need Java 8 to use Neo4j.|
The Example Project
The Neo4j example project is a small, one page webapp for the movies database built into the Neo4j tutorial. The front-end page is the same for all drivers: movie search, movie details, and a graph visualization of actors and movies. Each backend implementation shows you how to connect to Neo4j from each of the different languages and drivers.
Neo4j Java Driver
The Neo4j Java driver is officially supported by Neo4j and connects to the database using the binary protocol. It aims to be minimal, while being idiomatic to Java.
When using Maven, add this to your pom.xml file:
For Gradle or Grails, this is how to add the dependency:
For other build systems, see information available at Maven Central.
The Neo4j Team
Spring Data Neo4j
Neo4j’s Spring Data integration was the founding project of the Spring Data efforts, started by Rod Johnson and Emil Eifrem. Spring Data Neo4j was recently rewritten to incorporate the many improvements in Neo4j and Cypher but also in the Spring Data ecosystem.
It integrates tightly with the Spring Framework and offers Object-Graph mapping on top of Neo4j.
Features (based on Neo4j-OGM):
- Spring Boot Integration (since 1.4)
- annotation based object graph mapping
- interface based repository support with annotated and derived finder methods
- fast class metadata scanning
- optimized management of data loading and change tracking for minimal data transfers
- multiple transports: binary protocol, HTTP and embedded
- persistence lifecycle events
More detail can be found on the dedicated Spring Data Neo4j page.
Object-Graph-Mapping with Neo4j-OGM
Developing Java Business Applications often requires mapping rich domain models to your database. The Neo4j-OGM library is a pure Java library that can persist (annotated) domain objects using Neo4j. It uses Cypher statements to handle those operations in Neo4j.
The OGM supports tracking changes to minimize necessary updates and transitive persistence (reading and updating neighborhoods of an object).
The connection to Neo4j handled by a driver layer, which can use the binary protocol, HTTP or Neo4j’s embedded APIs.
The Neo4j and GraphAware Teams
Using Neo4j’s Embedded Java API
Being implemented in Java you can use Neo4j’s Java APIs also directly in your programs, much like HSQL or Derby. This is great for unit testing, but also useful for very high performance requirements and single-tier setups.
Besides an object-oriented API to the graph database, working with
Path objects, it also offers highly customizable, high-speed traversal- and graph-algorithm implementations.
We don’t provide extensive code examples for the Java API on this page, because they are covered in detail in the Java developers manual and other articles (please adapt the cited Neo4j version to the one you use).
As a JVM developer you are also on the bright side when it comes to inserting large amounts of data into Neo4j. With its non-transactional, highly-concurrent batch-insertion APIs it can ingest billions of nodes and relationships in just minutess.
Because of its flexibility and performance, we also chose the Java API for extending Neo4j as explained below.
User Defined Procedures
User Defined Procedures are now available within Cypher and encapsulate dedicated functionality.
Just by annotating methods of a Java class with
@Procedure and deploying the resulting jar file into your Neo4j installation, you can make new functionality easily available within the query language.
More useful examples for user defined procedures can be found in our neo4j-contrib GitHub repository.
Extending Neo4j Server with a REST Server Extension
Another extension point are Neo4j Server REST extensions. They encapsulate certain graph or business functionality behind a domain specific REST protocol which can be accessed using HTTP requests against the mounted extension points.
After declaring the dependencies to Neo4j and Neo4j-Server-APIs you can create REST-resources that are made available by Jersey at startup.
Within those resources you can get access to all Neo4j Java APIs via the injected dependencies to perform any graph operation.
Their results are then collected and rendered in the required format, e.g. JSON, XML, plain text or binary.
You test your server extensions using the neo4j-harness tooling which allows to start an in-process Neo4j server to test against.
This example project shows how you would do that for our movie application endpoints.
Using Neo4j Server with JDBC
Being a Java developer you’re probably familiar with JDBC as a way to work with relational databases, either directly or via abstractions like Spring’s
JDBCTemplate or MyBatis. Many tools for ETL, Report Generators, Business Intelligence, and data management also use JDBC drivers to interact with relational databases.
As Cypher, like SQL, is a textual parametrizable query language that can return tabular results, it was possible for us to support the JDBC APIs and make a Neo4j-JDBC driver available.
The driver supports the new binary “Bolt” protocol of Neo4j 3.x, the transactional http endpoint and Neo4j embedded connections.
More details on how to use it can be found with our example project for Java-JDBC. There, we implement the backend as a minimal Java webapp that leverages Neo4j-JDBC to connect to Neo4j Server.
Developers from Larus BA Italy and Neo4j
Neo4j Community Drivers
|The drivers below have been thankfully contributed by the Neo4j community. Many of these are fully featured and well maintained. But we don’t take any responsibility for their fitness for use with the most recent versions of Neo4j.|
JCypher provides seamlessly integrated Java access to Neo4j at different levels of abstraction:
At the topmost level of abstraction, JCypher allows you to map complex business domains to graph databases. You can take an arbitrarily complex graph of domain objects or POJOs and store it in a straightforward way into Neo4j for later retrieval. You do not need to modify your domain object classes in any way, there is also no need for annotations. JCypher provides a default mapping out of the box.
At the same level of abstraction, Domain Queries provide the power and expressiveness of queries on a graph database, while being formulated on domain objects or on types of domain objects, respectively. The true power of Domain Queries comes from the fact, that the graph of domain objects is backed by a graph database.
At the next lower level of abstraction – access to graph databases – is provided based on a generic graph model. While simple, the model allows you to easily navigate and manipulate graphs.
At the bottom level of abstraction, a native Java DSL in the form of a fluent Java API allows you to intuitively and comfortably formulate queries against Neo4j.
Groovy & Grails: Neo4j Grails Plugin
The goal of GORM for Neo4j is to provide a ‘as-complete-as-possible’ GORM implementation that maps domain classes and instances to the Neo4j nodespace. The following features are supported:
- Marshalling from Neo4j Nodes to Groovy types and back again
- Support for GORM dynamic finders, criteria and named queries
- Session-managed transactions
- Access to Neo4j’s traversal capabilities
- Access the Neo4j graph database in all flavours (Embedded, REST and HA)
Stefan Armbruster, Graeme Rocher
An idiomatic, feature-rich Clojure client which supports (almost) all Neo4k REST API features and is constantly tested against bleeding edge server changes, like the Cypher language improvements.
Michael Klishin, Rohit Aggarwal
A Cypher-oriented Scala library modeled after the Play! framework’s Anorm library.
JPA: Hibernate OGM
Hibernate Object/Grid Mapper (OGM) with Neo4j Support.
Davide D’Alto, Gunnar Moelling, Emmanuel Bernard