title: Spring Framework 5 -How to stop entity setter methods from flushing changes to databases?
body: <p>I am working on migrating a project from spring framework 4 to 5.</p>
  <p><strong>Spring Framework 4 Behavior:</strong> 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.<br />
  For instance, the following changes will be flushed into db.</p>
  <pre class="lang-java prettyprint-override"><code>    // Example 1
      @Transactional
      public void updateUser() {
          User user = userService.get(2L);
          user.setName(&quot;dummy&quot;);
      }
  </code></pre>
  <p>And the following changes won't get flashed to db.</p>
  <pre class="lang-java prettyprint-override"><code>    // Example 2
      public void updateUser() {
          User user = userService.get(2L);
          user.setName(&quot;dummy&quot;);
      }
  </code></pre>
  <p><strong>Spring Framework 5 Behaviro:</strong> 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.</p>
  <p>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.</p>
  <p>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.</p>