How to update an entity with Spring Data JPA
Codemia Knowledge Hub ·
Fetch the entity through its repository, change the fields you need, and call save(). You need an entity instance that was either loaded from the database or rebuilt with a known ID; once you modify its attributes, saving persists the changes.
The flow has three steps:
- Fetch the entity with the repository, for example
findById(id). - Update the entity by calling its setters.
- Save the entity back to the database with
save(entity).
Example: updating a user's email
Given a User entity with id, name and email, this service updates the email:
If the user does not exist, orElseThrow raises an EntityNotFoundException. Handle that exception where the update is called.
Running the update in a transaction
Put @Transactional on service methods that update entities so the database work runs inside a transaction. That protects data integrity and lets Spring roll back on failure.
Preventing lost updates with @Version
For concurrent updates, add a version field annotated with @Version. JPA increments it on every update, which gives you optimistic locking and prevents one writer from silently overwriting another.
Updating only changed columns with @DynamicUpdate
On entities with many fields, Hibernate's @DynamicUpdate annotation (compatible with Spring Data JPA) makes the generated SQL UPDATE include only the modified columns.
When to skip loading the entity
- Partial updates: If only a few fields change, a
@Querywith an update statement avoids the overhead of fetching the full entity. - Validation: Validate the entity or the updated fields before saving to avoid data integrity problems.
| Aspect | Description | Example/Keyword |
| Entity Retrieval | Fetch existing entity from database via repository. | findById(id) |
| Entity Modification | Update attributes of the entity. | entity.setField(val) |
| Save Changes | Persist changes back to the database. | save(entity) |
| Transaction Handling | Ensure method executes within a transaction. | @Transactional |
| Concurrency Control | Prevent overwriting updates in concurrent settings. | @Version |
| Optimized Updating | Only update changed fields in SQL. | @DynamicUpdate |
Related reading
- How do I update elements of a tensor using indices?
- How do I update if exists, insert if not AKA upsert or merge in MySQL?
- How do I use TTL on clickhouse table?
- How do I view the SQL generated by the Entity Framework?
- How do I update the element at a certain position in an ArrayList?
- How do I use a PriorityQueue?
- How do I write LINQ's .Skip1000.Take100 in pure SQL?
- How do I write one to many query in Dapper.Net?
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.