Spring Framework 5 -How to stop entity setter methods from flushing changes to databases?

Viewed 20

I am working on migrating a project from spring framework 4 to 5.

Spring Framework 4 Behavior: In the older version of the project, if I changed the value of a managed entity object via its setter method hibernate would only flush to db if the changes were made within a @Transactional annotated method. Otherwise, the flush to db wouldn't ever happen.
For instance, the following changes will be flushed into db.

    // Example 1
    @Transactional
    public void updateUser() {
        User user = userService.get(2L);
        user.setName("dummy");
    }

And the following changes won't get flashed to db.

    // Example 2
    public void updateUser() {
        User user = userService.get(2L);
        user.setName("dummy");
    }

Spring Framework 5 Behaviro: But in spring framework 5 even if I change the value of a managed entity object via its setter method within a method that is not annotated with @Transactional, hibernate would flush the changes to db. If we consider the above examples both example 1 and 2 will flush the change to db.

I have searched in Stack Overflow about this issue and found that this is excepted behavior in the newer version according to the documentation. But my problem is that the project I am migrating is huge and it is not possible to search and change every file to mitigate this issue.

So I am searching for a solution to get back the old behavior of spring @Transactional annotation in the newer version of the spring framework which will only trigger the flush to db action only if the @Transactional annotation is defined on a method.

0 Answers
Related