title: Spring JPA: How to upsert without losing data
body: <p>Is there any way for insert a new record if doesn't exist and update the record if exist without losing old data?</p>
  
  <p>This is my service layer method:</p>
  
  <pre><code>public void saveSample(Sample sample) {
      Sample samplePersistent = sample;
  
      if (sample.getId() != null) {
          samplePersistent = sampleRepository.findOne(sample.getId());
          Assert.notNull(samplePersistent, "Sample entity not found with id : " + sample.getId());
  
          samplePersistent.setLocation(sample.getLocation());
          samplePersistent.setName(sample.getName());
          samplePersistent.setType(sample.getType());
          ...
          ...
  
      }
  
      samplePersistent.cloneAuditingInfoFrom(sample);
      sampleRepository.save(sample);
  }
  </code></pre>
  
  <p>I think this is useless way.</p>
  
  <p>Can Spring BeanUtils Class or @DynamicUpdate Annotation solve my problem?</p>