This example shows that a Spring Boot application built with Spring Data Neo4j works as a drop-in replacement against Memgraph. Memgraph speaks the Bolt protocol and is wire-compatible with the Neo4j Java driver, so the only thing that changes versus a Neo4j project is the connection URI.
It is modeled on the official
Spring Data Neo4j getting-started guide
and reuses the same Movie / Person domain so you can compare them side by side.
- Connects Spring Data Neo4j to Memgraph over Bolt (
bolt://localhost:7687). - Maps
:Movieand:Personnodes (and theACTED_IN/DIRECTEDrelationships) with the standard@Node,@Id,@Propertyand@Relationshipannotations. - On startup, SchemaInitializer.java
creates label indexes (
:Movie,:Person) and label-property indexes (:Movie(title),:Person(name)), then aCommandLineRunner(DataLoader.java) loads a small movie graph and queries it — both a derived query and an explicit@QueryCypher statement. - Exposes a small REST API (MovieController.java) to load and query data over HTTP.
src/main/java/com/memgraph/bestpractices/springboot/
├── SpringBootMemgraphApplication.java # Spring Boot entry point
├── SchemaInitializer.java # creates indexes on startup
├── DataLoader.java # loads + queries data on startup
├── entities/
│ ├── MovieEntity.java
│ └── PersonEntity.java
├── repositories/
│ └── MovieRepository.java
└── controllers/
└── MovieController.java
src/main/resources/
└── application.properties # <-- the only Memgraph-specific config
A docker-compose.yml is provided with Memgraph (MAGE)
and Memgraph Lab:
docker compose up -dThis starts Memgraph on Bolt port 7687 and Memgraph Lab on
http://localhost:3000.
Prefer a single container? You can also run:
docker run -it --rm -p 7687:7687 memgraph/memgraph-mage:3.10.1- Java 17+ (set in
pom.xmlviajava.version) - Maven 3.6+ (or use the Maven wrapper if you generate one)
Dependencies are declared in pom.xml — the standard Spring Boot
starters spring-boot-starter-data-neo4j and spring-boot-starter-web. No
Memgraph-specific library is needed.
- Start Memgraph:
docker compose up -d - Run the app:
mvn spring-boot:run
On startup you should see log lines like:
Loaded 1 movie(s) into Memgraph
Found movie 'The Matrix' with 2 actor(s) and tagline "Welcome to the Real World"
Keanu Reeves acted in: The Matrix
# List all movies
curl http://localhost:8080/movies
# Fetch one movie by title
curl "http://localhost:8080/movies/by-title?title=The%20Matrix"
# Movies a given actor played in
curl "http://localhost:8080/movies/by-actor?name=Keanu%20Reeves"
# Create / update a movie
curl -X PUT http://localhost:8080/movies \
-H "Content-Type: application/json" \
-d '{"title":"John Wick","description":"People keep asking if I am back"}'
# Delete a movie by title
curl -X DELETE "http://localhost:8080/movies/John%20Wick"The whole point: switching a Spring Data Neo4j app from Neo4j to Memgraph is a
config change. In application.properties:
# Neo4j Aura
spring.neo4j.uri=neo4j+s://<instance>.databases.neo4j.io
# Memgraph
spring.neo4j.uri=bolt://localhost:7687A few Memgraph-specific notes:
- Authentication is off by default in Memgraph, so the username/password properties are left blank. Fill them in if you created a user.
- Do not set
spring.data.neo4j.database. Memgraph has a single database; setting it makes the driver issueUSE <db>, which Memgraph rejects. - Prefer an assigned business key for
@Id(heretitle/name) rather than relying on internal node ids. - Create indexes — Memgraph creates none out of the box. You need both a
label index (
CREATE INDEX ON :Movie) so plainMATCH (m:Movie)scans fromfindAll/count/deleteAlldon't sweep the whole store, and a label-property index (CREATE INDEX ON :Movie(title)) for the@Idlookups SDN's generated Cypher uses. Without them you'll seePlanHintingsequential-scan warnings. This example creates both automatically in SchemaInitializer.java.
This example was built and tested with:
- Memgraph v3.10.1 (
memgraph/memgraph-mage:3.10.1) - Spring Boot 3.3.4 / Spring Data Neo4j 7.3.4
- Java 17
⚠️ Use Memgraph 3.10.1 or newer. On older versions (e.g. 3.2) the relationship-persistence query that Spring Data Neo4j generates could terminate the server connection — this was verified fixed on 3.10.1.
If you run into any issues or have questions, feel free to reach out on the Memgraph Discord server. We're happy to help!
✅ This example runs on Memgraph Community Edition.