title: The entity field value is updated after exception throwing and transaction rollback
body: <p>I have developed a test project to reproduce this issue.</p>
  <p>This is a project structure:</p>
  <p><a href="https://i.stack.imgur.com/TI7ID.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TI7ID.png" alt="The project structure" /></a></p>
  <p>pom.xml file:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
           xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
           xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.7.1&lt;/version&gt;
          &lt;relativePath/&gt;
      &lt;/parent&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;org.example&lt;/groupId&gt;
      &lt;artifactId&gt;value-updated-after-fail-spring&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
      &lt;properties&gt;
          &lt;maven.compiler.source&gt;17&lt;/maven.compiler.source&gt;
          &lt;maven.compiler.target&gt;17&lt;/maven.compiler.target&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.h2database&lt;/groupId&gt;
              &lt;artifactId&gt;h2&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
              &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
  &lt;/project&gt;
  </code></pre>
  <p>Persone.java file:</p>
  <pre><code>@Entity
  @NoArgsConstructor(access = AccessLevel.PROTECTED)
  @RequiredArgsConstructor
  @AllArgsConstructor
  @Builder
  @Getter
  @Setter
  @ToString
  @FieldDefaults(level = AccessLevel.PRIVATE)
  public class Person {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      Long id;
  
      @Column(nullable = false)
      @NonNull
      String name;
  }
  </code></pre>
  <p>PersonRepository.java file:</p>
  <pre><code>@Repository
  public interface PersonRepository extends JpaRepository&lt;Person, Long&gt; {}
  </code></pre>
  <p>PersonService.java file:</p>
  <pre><code>@Component
  public class PersonService {
  
      private final PersonRepository repository;
  
      public PersonService(PersonRepository repository) {
          this.repository = repository;
      }
  
      @Transactional
      public Person create(String name) {
          return repository.save(new Person(name));
      }
  
      @Transactional
      public Person save(Person person) {
          if(StringUtils.isBlank(person.getName())) {
              throw new RuntimeException();
          }
          Person personFromDB = getById(person.getId());
          personFromDB.setName(person.getName());
          return repository.save(personFromDB);
      }
  
      @Transactional
      public Person getById(Long id) {
          return repository.findById(id)
                  .orElseThrow(NullPointerException::new);
      }
  
      @Transactional
      public void deleteAll() {
          repository.deleteAll();
      }
  }
  </code></pre>
  <p>application.properties file:</p>
  <pre><code>spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
  spring.datasource.username=sa
  spring.datasource.password=
  spring.jpa.hibernate.ddl-auto=create-drop
  spring.jpa.show-sql=true
  spring.h2.console.enabled=true
  spring.datasource.driver-class-name=org.h2.Driver
  spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
  </code></pre>
  <p>TestApplicationConfiguration.java file</p>
  <pre><code>@SpringBootConfiguration
  @EnableAutoConfiguration
  @EnableJpaRepositories
  @EntityScan(&quot;net.example.model&quot;)
  @ComponentScan(basePackages = &quot;net.example&quot;)
  public class TestApplicationConfiguration {}
  </code></pre>
  <p>PersonServiceTest.java file:</p>
  <pre><code>@DataJpaTest
  @AutoConfigureTestDatabase(replace = Replace.NONE)
  class PersonServiceTest {
  
      @Autowired
      private PersonService service;
  
      @AfterEach
      void tearDownEach() {
          service.deleteAll();
      }
  
      @Test
      void rename() {
          String expected = &quot;name&quot;;
          Person person = service.create(expected);
          Person personFromDB = service.getById(person.getId());
          personFromDB.setName(&quot;&quot;);
          assertThrows(RuntimeException.class, () -&gt; service.save(personFromDB));
          assertEquals(expected, service.getById(personFromDB.getId()).getName());
      }
  }
  </code></pre>
  <p><strong>The issue:</strong> Last assertion fails</p>
  <pre><code>org.opentest4j.AssertionFailedError:
  Expected :name
  Actual   :
  </code></pre>
  <p><strong>What I already tried to fix this?</strong></p>
  <ol>
  <li>I tried to remove the <code>@Transactional</code> annotation for the PersonService#getById method to avoid getting the entity from the cache. - This didn't fix the issue</li>
  <li>I tried to add <code>spring.cache.type=none</code> to the <code>application.properties</code> file to disable the cache. - This didn't fix the issue</li>
  </ol>
  <p><strong>Why do I think it's the cache?</strong></p>
  <p>When I debugged this, I found that the <code>PersonService#getById()</code> method doesn't return actual data, but the method returns a cached object with a changed title.
  The database isn't changed after calling the <code>PersonService#save</code> method because it throws an exception
  <a href="https://i.stack.imgur.com/W3tEB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/W3tEB.png" alt="enter image description here" /></a></p>
  <p>Perhaps I'm not developing the tests correctly.
  Maybe I should change the method of saving changed data.</p>
  <p>Please share best practices and articles to better understand how to update data and how to properly configure and write tests for Spring Boot applications.</p>