beginnerSpring Data JPA & Hibernate
What's the difference between `save()` triggering an INSERT vs an UPDATE in Spring Data JPA?
`save()` checks whether the entity's `@Id` is null (or matches Hibernate's configured 'new entity' detection) — if so, it INSERTs; if the ID is already set and Hibernate believes the row exists, it UPDATEs (technically via a SELECT-then-decide, or directly if the entity is 'detached' and assumed persistent). This is why passing an entity with a manually-set ID that doesn't exist yet can behave unexpectedly depending on the ID generation strategy.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response