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.

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Spring Ecosystem Mastery library.

What's the difference between `save()` triggering an INSERT vs an UPDATE in Spring Data JPA?

Next Step

Continue to Why does Hibernate default `@OneToMany` to `FetchType.LAZY` but `@ManyToOne` to `FetchType.EAGER`?← Back to all Spring Boot questions