I have a simple controller method, where I create a new object Car and then set its name to Audi:
@GetMapping(value = "/resource")
public ResponseEntity visit() {
Car car = carRepo.save(new Car("VolksWagen")); // Car should be managed now?
car.setName("Audi"); // <-- has no effect on database state
return ResponseEntity.ok().build();
}
In the database, it never becomes an Audi, but stays a VolksWagen.
Why does this happen? Shouldn't the newly created Car be in managed state for the persistence context?
Note: It works if I add the @Transactional annotation. I thought it would be enough if OSIV is enabled. What am I misunderstanding about OSIV and @Transactional?
carRepo.save do a persist or a merge? if you are using merge pick up the result of the merge!
"Persist takes an entity instance, adds it to the context and makes that instance managed (ie future updates to the entity will be tracked).
Merge creates a new instance of your entity, copies the state from the supplied entity, and makes the new copy managed. The instance you pass in will not be managed (any changes you make will not be part of the transaction - unless you call merge again)." as described in this answer