Card 04/ 06

GotchaDifficulty: Advanced1 min

save() on an Existing Id Updates It

save() reads as though it always creates something new. Called on an entity that already has an id, it does the opposite.

java
RideEntity existing = repo.findById(id).orElseThrow();
existing.status = "completed";
repo.save(existing);
text
save() on existing id: count before=3 after=3 (same id updated, not inserted)
run in a container — the row count never changes

save() checks whether the entity's id is already set. If it is, Spring Data issues an UPDATE against that row instead of an INSERT — which is exactly the behaviour that makes "load, modify, save" the normal pattern for changing a row, rather than a separate update() method existing at all.