Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To update a mapped Neo4j entity in Spring Data Neo4j (SDN), load the existing entity, change its mapped fields, and call repository.save(entity) inside a Spring-managed transaction. Use explicit Cypher for targeted or bulk writes, and add optimistic locking with @Version Long when concurrent updates could overwrite each other.

Update an existing entity with a repository

For an ordinary aggregate update, first retrieve the entity so it has its existing identifier and mapped state. Change the property, then save it:

@Service
class PersonService {
  private final PersonRepository repository;

  @Transactional
  Person rename(long id, String newName) {
    Person person = repository.findById(id)
        .orElseThrow(() -> new NoSuchElementException("Person not found"));
    person.setName(newName);
    return repository.save(person);
  }
}

Repository save is SDN’s usual high-level route for persisting a mapped aggregate. The repository abstraction and generated persistence queries are designed to work with domain entities; custom queries are available when generated persistence is not the right fit. See the Spring Data Neo4j reference.

Keep the read, modification, and write in a Spring-managed transaction when they are one business operation. Repositories, Neo4jTemplate, and Neo4jClient participate in Spring application transactions. If you use the Bolt driver directly, you are responsible for the transaction boundary. Transaction and API details are in the SDN reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the right update API

Approach Best for Mapping and control Transaction boundary
Repository save Ordinary updates to a loaded entity and its modeled aggregate Highest-level mapped persistence; least statement-level control Integrates with Spring application transactions
Neo4jTemplate Programmatic mapped operations that go beyond a repository method Retains template mapping support Integrates with Spring application transactions
Neo4jClient Explicit Cypher with lower-level control over the statement and results Mapping-agnostic; map results yourself Integrates with Spring application transactions
Repository @Query Targeted property changes, bulk updates, or query shapes generated persistence does not express Explicit Cypher; return mapping and annotations depend on SDN version and query shape Use within the Spring transaction for the business operation
Direct Bolt driver Driver-level work outside SDN’s mapped APIs Explicit Cypher and caller-managed result handling You manage transactions

Spring Data Neo4j describes itself as providing configuration and access to Neo4j databases from Spring applications, with repositories and other APIs supporting different levels of abstraction. Project overview · Reference documentation.

Target one property with Cypher

A custom repository query can be clearer than loading and saving an aggregate when only one property needs to change, or when the write is naturally expressed as a bulk operation. Adapt labels and property names to your model:

@Modifying
@Query("MATCH (p:Person {id: $id}) SET p.name = $name RETURN p")
Person updateName(long id, String name);

Do not assume this exact method signature or annotation combination works unchanged in every SDN release. Result mapping and annotation requirements depend on the version and query shape; check the reference matching the version used by your project.

Check your mapping when a save appears not to update the expected data

SDN persists Java or Kotlin attributes according to the entity mapping. Those mappings determine whether data is stored as node properties, relationship properties, or related graph objects:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Attributes on a @Node class map to node or relationship properties using the attribute name by default. Use @Property("db_name") when the stored property name differs.
  • @Relationship maps references to other @Node types, including collections and maps. Relationships point outward by default; dynamic relationships can use a map keyed by relationship type.
  • If a relationship has its own data, model it with @RelationshipProperties and a @TargetNode. Change the relationship-properties entity to update that relationship’s data; changing an endpoint node’s scalar field is not the same operation.

See the SDN object-mapping reference for the mapping annotations and behavior.

Saving an object that was constructed without first loading the existing aggregate can omit relevant mapped state from the operation. Load the entity and modify it for a normal aggregate update; choose explicit Cypher when a narrow, statement-level change is more appropriate.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Prevent lost updates with optimistic locking

When concurrent writers may change the same entity, add a @Version field typed as Long:

@Node
class Person {
  @Id @GeneratedValue
  private Long id;

  @Version
  private Long version;

  private String name;
}

SDN increments the version automatically after a successful update; do not change it yourself. If two transactions read version x, the first successful update advances it to x+1. The competing update based on the stale version fails with OptimisticLockingFailureException. The SDN reference documents optimistic locking.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recover from a conflict

  1. Catch or otherwise handle OptimisticLockingFailureException at the service boundary appropriate to your application.
  2. Reload the entity so you have the current version and state.
  3. Reapply the business operation to that fresh state, then attempt the save again according to your retry policy.

Do not blindly replay a stale entity or manually increment its version: the conflict signals that another update succeeded, and the business operation may need to be reconsidered against the new state.

Check the SDN version used by your project

The Spring Data/Broadcom release information lists Spring Data Neo4j 8.1.1 as stable in 2026; 8.0.7 and 7.5.13 are also listed as stable lines, while 8.2.0-M1 is a preview release. These versions are not interchangeable instructions for every project: verify your Spring Data release train and consult its matching reference before copying dependencies or custom-query examples. Release and project information.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.