title: How to update one field in my Entity using JPA Repository
body: <p>I have Entity with 3 fields: id, lastname and phoneNumber. I want to create method which works for update all fields or only one or two.</p>
  
  <p>I use Hibernate and JPA Repository.
  When I try to update all fields everything works well but when for example  i want to update only lastname without changing of phoneNumber I have in output null insted of old phoneNumber.</p>
  
  <p>Here is my method from Controller:</p>
  
  <pre><code>@PutMapping("/students/update/{id}")
  public String updateStudentById(@ModelAttribute Student student, @ModelAttribute StudentDetails studentDetails,
                                  String lastname, String phoneNumber,
                                  @PathVariable Long id) {
  
      Optional&lt;Student&gt; resultOptional = studentRepository.findById(id); 
  
      //Student result =resultOptional.get();
      resultOptional.ifPresent((Student result) -&gt; {
             result.getStudentDetails().setPhoneNumber(studentDetails.getPhoneNumber());     result.getStudentDetails().setLastname(studentDetails.getLastname());
          studentRepository.save(result);
      });
      return "Student updated";
  }
  </code></pre>
  
  <p>The class for update:</p>
  
  <pre><code>@DynamicUpdate
  @Entity
  public class StudentDetails {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      @Column(name="lastname")
      private String lastname;
      @Column(name="phone_number")
      private String phoneNumber;
  
      public StudentDetails() {
      }
  
      public StudentDetails(Long id, String lastname, String phoneNumber) {
          this.id = id;
          this.lastname = lastname;
          this.phoneNumber = phoneNumber;
      }
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getLastname() {
          return lastname;
      }
  
      public void setLastname(String lastname) {
          this.lastname = lastname;
      }
  
      public String getPhoneNumber() {
          return phoneNumber;
      }
  
      public void setPhoneNumber(String phoneNumber) {
          this.phoneNumber = phoneNumber;
      }
  }
  </code></pre>
  
  <p>The class which has relation with StudentDetails:</p>
  
  <pre><code>@Entity
  @Table(name = "student")
  @DynamicUpdate
  public class Student {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name = "name")
      private String name;
      @Column(name = "email")
      private String email;
  
      //@OneToMany(mappedBy = "student")
      @ManyToMany
      @JoinTable(name="course_student",joinColumns = @JoinColumn(name="student_id"),
      inverseJoinColumns = @JoinColumn(name="course_id"))
  
      private List&lt;Courses&gt; courses;
  
      @OneToOne(cascade = CascadeType.ALL)
     // @JoinColumn(name="studen/_details_id") // with this we have dobule student_details column
      private  StudentDetails studentDetails;
  
  
      public List&lt;Courses&gt; getCourses() {
          return courses;
      }
  
      public void setCourses(List&lt;Courses&gt; courses) {
          this.courses = courses;
      }
  
      public StudentDetails getStudentDetails() {
          return studentDetails;
      }
  
      public void setStudentDetails(StudentDetails studentDetails) {
          this.studentDetails = studentDetails;
      }
  
      // Methods for StudentViewController
      public String getLastname(){
          return studentDetails.getLastname();
      }
      public String getPhoneNumber(){
          return studentDetails.getPhoneNumber();
      }
  
      public Student() {
      }
  
      public Student(String name, String email, StudentDetails studentDetails) {
         // this.id = id;
          this.name = name;
          this.email = email;
          this.studentDetails = studentDetails;
      }
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      @Override
      public String toString() {
          return "Student{" +
                  "id=" + id +
                  ", name='" + name + '\'' +
                  ", email='" + email + '\'' +
                  '}';
      }
  
  }
  </code></pre>
  
  <p>I was looking for solution and I added @DynamicUpdate but still it doesn't work.</p>