Card 04/ 06
All 6 cards
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.
RideEntity existing = repo.findById(id).orElseThrow();
existing.status = "completed";
repo.save(existing);save() on existing id: count before=3 after=3 (same id updated, not inserted)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.