'Disable auto update of dirty entities in Hibernate
Is there any way to disable auto-update of dirty entities completely?
In our application we load an entity and some 'simulation' business logic changes an attribute (for hibernate this entity is now dirty).
Afterwards we want to save the simulation-results to the database. This we do by explicitly calling resultRepo.save(result). In our whole project we use this paradigm that we call explicitly *Repo.save(..) for entities we want to save.
The problem is that if transaction ends Hibernate now automatically also saves the dirty entity. How can we avoid this implicit magic?
Possible solutions:
Transactional(readOnly=true)doesn't work since we need to store the results.- manually detach entity (or clone required data) into new object --> very unhandy since we have this situation on so many places.
- Setting hibernate flushMode to
MANUAL-> now everything works as expected but- I don't really know why?
- It feels like a hack because I'm trying so configure dirty-auto-save and using flush mode for this.
Ideally there would be an explicit toggle like org.hibernate.autoSaveDirtyEntites: false.
In case it madders: We use Hibernate 5.6.5 on Spring Boot 2.6.4.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
