title: Spring Cache with Spring Data Rest Repository is caching results of save on exception
body: <p>I have a Spring Data Rest repository that I would like to use Spring Caching with it.</p>
  
  <p>The cache works as expected when there are no errors.  However, when the result of the save is a validation exception, the entity is cached, even though it is not written to the database. </p>
  
  <p>SDR Repository</p>
  
  <pre><code>@RepositoryRestResource(collectionResourceRel = "businessUnits", path = "businessUnits")
  public interface BusinessUnitRepository extends JpaRepository&lt;BusinessUnit, UUID&gt; {
  
  @Override
  @Cacheable(value = "lookups", cacheManager = "vaultCache")
  BusinessUnit findOne(final UUID id);
  
  @Override
  @CacheEvict(value = "lookups", cacheManager = "vaultCache", allEntries = true, beforeInvocation = true)
  BusinessUnit save(BusinessUnit entity);
  </code></pre>
  
  <p>If I POST the message body below a new Business Unit it is saved correctly.  The first GET to retrieve the record hits the database as expected and subsequent GETs come from the cache. </p>
  
  <pre><code>{
  "name": "Test_Business_2",
  "effectiveDate": "2019-12-16T11:11:11.111+0000",
  "expirationDate": "2020-12-16T11:11:11.111+0000",
  "businessUnitType": "/businessUnitTypes/38faf33c-5454-4245-bc69-2b31e510fa6b"
  }
  </code></pre>
  
  <p>The BusinessUnit Entity has a not null on the effectiveDate field.</p>
  
  <pre><code>@NotNull
  @Column(name = "effective_date")
  private Timestamp effectiveDate;
  </code></pre>
  
  <p>When I PATCH the business unit with a null effectiveDate.  The response is an error, an exception is logged and the database is not updated.</p>
  
  <pre><code>{
  "effectiveDate": null
  }
  </code></pre>
  
  <p>When I query the db, I see the record was not updated</p>
  
  <pre><code>NAME            EFFECTIVE_DATE      EXPIRATION_DATE
  Test_Business_2,2019-12-16 06:11:11,2020-12-16 06:11:11
  </code></pre>
  
  <p>However, when I do a GET on the businessUnits endpoint.  The database is not hit to read the record, and the null effectiveDate is returned. </p>
  
  <pre><code>{
  "createDate": "2017-02-20T13:38:00.386+0000",
  "lastModifiedDate": "2017-02-20T13:38:00.386+0000",
  "effectiveDate": null,
  "expirationDate": "2020-12-16T11:11:11.111+0000",
  "name": "Test_Business_2",  ...
  </code></pre>
  
  <p>So, it seems the data that caused the exception to be thrown, is stored in cache.</p>
  
  <p>What do I need to change so that the value is not saved in cache when the database is not updated on save?</p>
  
  <p>I appreciate any help or advice.  Thanks.</p>