title: Spring boot JPA update to update only specific fields
body: <p>So I encountered this issue with updating an entity in DB. while Passing a whole entity and updating only specific fields it treats untouched fields as null, as a result I get an exception since those fields are <code>@Not-Null</code>,</p>
  <p>I have tried looking for similar problems but could not fix my problem.</p>
  <p><strong>Company ENTITY:</strong></p>
  <pre><code>@Entity
  @Table (name = &quot;companies&quot;)
  @Data
  @ToString(exclude = &quot;perfumes&quot;)
  @AllArgsConstructor
  @NoArgsConstructor
  @Builder
  public class Company {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
  
      @NotNull
      private String name;
  
      @NotNull
      @Email(message = &quot;Wrong input. please enter a VALID email address&quot;)
      private String email;
  
      @NotNull
      @Size(min = 4, max = 14, message = &quot;Password range must be between 4 - 14 digits&quot;)
      private String password;
  
      @NotNull
      @Enumerated(EnumType.STRING)
      private Country country;
  
      @Singular
      @OneToMany(mappedBy = &quot;company&quot;, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      private List&lt;Perfume&gt; perfumes = new ArrayList&lt;&gt;();
  }
  </code></pre>
  <p>Most fields are <code>@NotNull</code> for creation, however, I need to update the entity, sometimes only specific fields.</p>
  <p><strong>Service:</strong></p>
  <pre><code>@Override
  public String updateCompany(int id, Company company) throws DoesNotExistException {
      if(!companyRepository.existsById(id))
      {
          throw new DoesNotExistException(id);
      }
  
      companyRepository.saveAndFlush(company);
      return company.getName() + &quot; has been UPDATED&quot;;
  }
  </code></pre>
  <p>as you can see an <code>ENTITY</code> has been passed which causes rest of attributes to be automatically null if not modified.</p>
  <p><strong>Controller:</strong></p>
  <pre><code>  @PutMapping(&quot;/updateCompany/{id}&quot;)
      @ResponseStatus(HttpStatus.ACCEPTED)
      public String updateCompany(@PathVariable int id, @RequestBody Company company) throws DoesNotExistException {
          return admin.updateCompany(id,company);
      }
  </code></pre>
  <p><strong>EXCEPTION:</strong></p>
  <pre><code>Validation failed for classes [com.golden.scent.beans.Company] during update time for groups [javax.validation.groups.Default, ]
  List of constraint violations:[
      ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=password, rootBeanClass=class com.golden.scent.beans.Company, messageTemplate='{javax.validation.constraints.NotNull.message}'}
  ]
  </code></pre>
  <p>Thanks.</p>