Neo4jTemplate is the imperative template in Spring Data Neo4j. It lets you save, load, count and delete mapped entities and run custom Cypher queries that are mapped back to your domain classes, without writing a repository interface.
It sits between the high-level repositories and the low-level Neo4jClient. Repositories are built on top of the same mapping infrastructure, so you can mix them freely in one application.
Spring Data Neo4j offers three levels of access:
| Level | Type | Use it for |
|---|---|---|
| Repositories | Neo4jRepository<T, ID> | Standard CRUD, derived queries, @Query methods |
| Template | Neo4jTemplate (implements Neo4jOperations) | Programmatic CRUD, dynamic Cypher with entity mapping, projections |
| Client | Neo4jClient | Arbitrary Cypher with manual result handling |
The template is blocking. For reactive applications the equivalent is ReactiveNeo4jTemplate. With the spring-boot-starter-data-neo4j dependency and a configured connection, Spring Boot creates a Neo4jTemplate bean for you.
The template works with classes annotated for the Neo4j mapping context.
@Node maps the class to a label, @Id marks the identity and @Relationship maps a relationship. Prefer a generated string id such as @GeneratedValue(UUIDStringGenerator.class) or your own business key over the internal Neo4j id, because internal ids can be reused after a node is deleted.
The most used methods of Neo4jTemplate are listed here.
| Method | Purpose |
|---|---|
save(T instance) | Create or update an entity and its mapped relationships |
saveAll(Iterable<T>) | Save several entities |
findById(Object id, Class<T>) | Return an Optional<T> |
findAll(Class<T>) | Return all entities of a type |
findAll(String cypher, Map<String, Object> parameters, Class<T>) | Run Cypher and map results to a type |
findOne(String cypher, Map<String, Object> parameters, Class<T>) | Same, expecting at most one result |
count(Class<?>) | Count nodes of a type |
deleteById(Object id, Class<?>) | Delete one entity |
deleteAll(Class<?>) | Delete all entities of a type |
Always pass values as query parameters ($name) and never concatenate them into the Cypher text. This prevents Cypher injection and lets Neo4j cache the query plan.
When your query returns nodes together with their relationships, return them as paths or return the relationships explicitly so the mapper can build the whole graph. A plain RETURN p loads only the node and its direct properties.
saveAs(instance, ProjectionType.class) saves only the properties defined by a projection, which avoids overwriting fields you did not mean to touch.
For results that do not map to an entity, such as aggregates, switch to Neo4jClient, which returns maps or custom mapped objects.
Wrap related operations in @Transactional so that Spring's Neo4jTransactionManager uses one Neo4j transaction. Spring Boot auto-configures this manager. Calls to save outside a transaction still work, but each runs in its own transaction.
Neo4jTemplate maps results to your entities and handles persistence of object graphs. Neo4jClient is lower level: you write Cypher and decide how to map every record.
Use repositories for standard CRUD and fixed queries. Use the template when queries are built dynamically, when you need projections, or when you do not want an extra interface.
Yes. It is a stateless singleton bean that you can inject anywhere. Transactions are bound to the calling thread.
Yes, ReactiveNeo4jTemplate offers the same operations returning Mono and Flux.
Neo4jTemplate gives you programmatic, entity-aware access to Neo4j in Spring Boot: save and load mapped objects, run parameterised Cypher that returns entities, and use projections for partial updates. Use @Transactional services around it and fall back to Neo4jClient for results that are not entities.