title: Are Nested/Child JPA Entities required to have a value when performing UPDATE operation
body: <p>I have a <code>Section</code> entity. <code>Section</code> entity has a nested <code>GradeLevel</code> entity annotated with <code>@OneToOne</code></p>
  <p><strong>Section</strong></p>
  <pre><code>@Entity
  @Setter
  @Getter
  public class Section{
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(unique = true, nullable = false)
      private String name;
  
      @OneToOne
      private GradeLevel gradeLevel;
  
      @Column(columnDefinition = &quot;boolean default true&quot;)
      private Boolean isActive = true;
  
      @CreationTimestamp
      @Column(name = &quot;date_created&quot;)
      private LocalDateTime dateCreated = LocalDateTime.now();
  
      @UpdateTimestamp
      @Column(name = &quot;date_last_updated&quot;)
      private LocalDateTime dateLastUpdated = LocalDateTime.now();
  
  }
  </code></pre>
  <p><strong>GradeLevel</strong></p>
  <pre><code>@Entity
  @Getter
  @Setter
  public class GradeLevel {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(unique = true, nullable = false)
      private String code; //GL-01, GL-02...
  
      @Column(unique = true)
      private String description; //Kinder 1, Kinder 2, Grade 1....
      private String category; //Elementary School, Junior Highschool, Senior Highschool
  
      @Column(columnDefinition = &quot;boolean default true&quot;)
      private Boolean isActive = true;
  
      @CreationTimestamp
      @Column(name = &quot;date_created&quot;)
      private LocalDateTime dateCreated = LocalDateTime.now();
  
      @UpdateTimestamp
      @Column(name = &quot;date_last_updated&quot;)
      private LocalDateTime dateLastUpdated = LocalDateTime.now();
  
  }
  </code></pre>
  <p>The <code>@OneToOne GradeLevel gradeLevel;</code> creates a foreign key in the <code>Section</code> table that references to <code>GradeLevel</code>'s <code>ID</code> column.</p>
  <p>The table looks like this:</p>
  <p><a href="https://i.stack.imgur.com/7LDDb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7LDDb.png" alt="enter image description here" /></a></p>
  <p>Now if I want to update just the <code>name</code> of the <code>Section</code> and pass just the Section <strong>ID</strong> and new Section <strong>Name</strong> <em>without the <strong>GradeLevel ID</strong></em> request sample below,</p>
  <blockquote>
  <p>{
  &quot;id&quot; : 1,
  &quot;name&quot; : &quot;Venus&quot; }</p>
  </blockquote>
  <p>The compiler complains about a <code>null</code> <code>GradeLevel</code> <code>ID</code></p>
  <pre><code>org.hibernate.HibernateException: identifier of an instance of com.jordan.cdoautoformsettingsservice.entities.GradeLevel was altered from 1 to null...
  </code></pre>
  <p>This gives me the impression that when we have nested JPA entities with <code>@OneToOne</code> (or <code>@OneToMany</code>...), we are required to provide the ID of the nested Entity. In this case, it wants the <code>ID</code> of the <code>GradeLevel</code></p>
  <p>I fails to complete the <strong>UPDATE</strong> operation.</p>
  <p><strong>ServiceImpl.java</strong> (UPDATE method) below</p>
  <pre><code>@Override
      public Section updateSection(Section request) throws Exception {
          logger.debug(&quot;request : &quot;+request);
          Long sectionId = request.getId();
          Optional&lt;Section&gt; optionalSection = sectionRepo.findById(sectionId); //retrieve the section we want to update
          if(!optionalSection.isPresent()){
              throw new Exception(&quot;Section with ID: &quot;+request.getId()+ &quot;is NOT FOUND&quot;);
          }
          GradeLevel gradeLevel = optionalSection.get().getGradeLevel();
          gradeLevel.setId(request.getGradeLevel().getId());
          logger.debug(&quot;GradeLevel Properties: &quot;);
          logger.debug(&quot;GradeLevel ID: &quot;+gradeLevel.getId());
          logger.debug(&quot;GradeLevel CODE: &quot;+gradeLevel.getCode());
          logger.debug(&quot;GradeLevel DESCRIPTION: &quot;+gradeLevel.getDescription());
          logger.debug(&quot;GradeLevel CATEGORY: &quot;+gradeLevel.getCategory());
  
          Section section = optionalSection.get();
          section.setName(request.getName()); //replace current section name with section name FROM REQUEST
          section.setDateLastUpdated(request.getDateLastUpdated());
          section.setIsActive(request.getIsActive());
          section.setGradeLevel(gradeLevel);
  
          return sectionRepo.save(section);
      }
  </code></pre>
  <p>I'd appreciate any suggestion or thoughts.</p>
  <p>Thank you.</p>