title: Update only the entity attributes passed from @RequestBody using Spring Rest Controller and Spring Data JPA
body: <p>How to update only the entity attributes passed from @RequestBody using Spring Rest Controller and Spring Data JPA ?</p>
  
  <p>Employee Entity:</p>
  
  <pre><code>@Entity
  @Table(name = "employee")
  @DynamicUpdate
  public class Employee {
      @Id
      @Column(name = "employee_id")
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer employeeId;
  
      @Column(name = "employee_name")
      private String employeeName;
  
      @Column(name = "employee_desgn")
      private String employeeDesgn;
  
      @Column(name = "employee_email")
      private String employeeEmail;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "company_id", referencedColumnName = "company_id")
      private Company company;
  }
  </code></pre>
  
  <p>Service Class Method:</p>
  
  <pre><code>public Employee updateEmployee(Employee employee) {
      return employeeRepository.save(employee);
  }
  </code></pre>
  
  <p>RequestBody:</p>
  
  <pre><code>{
      "employeeId":4,
      "employeeName":"Paul",
      "employeeDesgn":"Lead"
  }
  </code></pre>
  
  <p>Hibernate Update Query:</p>
  
  <pre><code>update employee set company_id=?, employee_desgn=?, employee_name=? where employee_id=?
  </code></pre>
  
  <p>Spring Data JPA is trying to set company_id as null to update even though I am not passing it through RequestBody ? 
  But if I got entity from database using employee_id passed and then if I tried to save() then its working fine. </p>
  
  <p>I would like to update only entity attributes which are passed through Request Body. How to handle this scenario ?</p>