title: JpaRepository merge() method
body: <p>I'm rewriting a big project with <code>SpringBoot 2.2.6</code> and I'm run into a problem.</p>
  <p>In the old project (pure <code>ejb</code>) when a complex entity is updated, the code build <code>entity</code> from <code>DTO's</code> as follows:</p>
  <pre><code>public Entity dtoToEntity(DTO dto) {
    Entity entity = new Entity();
    entity.setId(dto.getID());
    // ecc...
    // ecc...
    entity.setSubEntity(dto.getSubEntity() != null ? new SubEntity(dto.getSubEntity().getId() : null);
    // and so on
  }
  </code></pre>
  <p>The important section is that related to subentity! After made a mapping like that the old project calls:</p>
  <pre><code>EntityManager.merge(entity);
  </code></pre>
  <p>I think that with merge call, if inside the database exists a SubEntity with specified id and <strong>other fields valorized</strong>, the other fields remain valid and are not set to null because they aren't declared in mapping.</p>
  <p>But with <code>SpringBoot</code> I'm using <code>JpaRepository</code> and I don't think the same thing happens if I call:</p>
  <pre><code>jpaRepository.save(entity);
  </code></pre>
  <p>I think with this call the other fields of SubEntity with specified id will set to null!</p>
  <p>Is it correct?</p>
  <p>What can I solve this?</p>
  <hr />
  <p>Thanks for your reply first of all!</p>
  <p>You are right, I can't do something like that nor with EntityManager.merge() method! Than I try to explain better what I want to do:</p>
  <p>Suppose I have a complex Entity, which has many nested Entities (which may have nested entities) as follows:</p>
  <pre><code>  @Entity
    public class Car {
       private String name;
       ....
       ....
       private Engine engine; // nested entity
       private Chassis chassis; // nested entity
  }
  </code></pre>
  <p>And:</p>
  <pre><code>@Entity
  public class Engine {
  
    private String company;
    private Oil oil; // nested entity
    ....
  }
  </code></pre>
  <p>Now suppose in the database I have a Car, with all relationship filled (Engine, Chassis, Oil ecc..) and suppose I want to update the Car name from Ferrari to Fiat, if I use pure SQL I can simply write:</p>
  <pre><code>update Car c set c.name = &quot;Fiat&quot; where c.id = [id];
  </code></pre>
  <p>Now if I use Spring JPA, to ensure that all nested entity (and their field) are not setting to null when I update my entity I have to do:</p>
  <pre><code>Car car = carRepository.findById([id]);
  car.setName(&quot;Fiat&quot;):
  carRepository.save(car);
  </code></pre>
  <p>This way I will update Car name and I'm sure that all other entities will remain set because are loaded by findById() method.</p>
  <p>My question and my goal is to know if is there a way to do something like that:</p>
  <pre><code>Car car = new Car();
  car.setId(1); // id of Ferrari car
  car.setName(&quot;Fiat&quot;);
  someRepositoryOrEntityManager.saveOrUpdate(car);
  </code></pre>
  <p>And preserve all other field and relation without load all of these by the find method (maybe due to a performance reasons).</p>