title: JPA / Hibernate - Update parent entity on child changes
body: <p>I have following two entities in OneToOne relationship.</p>
  <pre><code>@Entity
  @Audited
  public class Parent implements Serializable {
  
      @Id
      @GeneratedValue(generator = &quot;system-uuid&quot;)
      @GenericGenerator(name = &quot;system-uuid&quot;, strategy = &quot;uuid2&quot;)
      @Size(max = 36)
      @Column(length = 36)
      private String id;
  
      @NotNull
      private String createdUser;
  
      @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
      @Temporal(TemporalType.TIMESTAMP)
      @NotNull
      private Date createdDate;
  
      private String modifiedUser;
  
      @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
      @Temporal(TemporalType.TIMESTAMP)
      private Date modifiedDate;
  
      @OneToOne(targetEntity = Child.class, optional = false, fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, orphanRemoval = true)
      @Fetch(FetchMode.JOIN)
      private Child child = new Child();
  
      @PrePersist
      public void prePersist() {
  
          createdDate = new Date();
          createdUser = &quot;SampleUser&quot;;
  
      }
  
      @PreUpdate
      public void preUpdate() {
  
          modifiedDate = new Date();
          modifiedUser = &quot;SampleUser&quot;;
  
      }
  
      public String getId() {
          return id;
      }
  
      public void setId(String id) {
          this.id = id;
      }
  
      public String getCreatedUser() {
          return createdUser;
      }
  
      public void setCreatedUser(String createdUser) {
          this.createdUser = createdUser;
      }
  
      public Date getCreatedDate() {
          return createdDate;
      }
  
      public void setCreatedDate(Date createdDate) {
          this.createdDate = createdDate;
      }
  
      public String getModifiedUser() {
          return modifiedUser;
      }
  
      public void setModifiedUser(String modifiedUser) {
          this.modifiedUser = modifiedUser;
      }
  
      public Date getModifiedDate() {
          return modifiedDate;
      }
  
      public void setModifiedDate(Date modifiedDate) {
          this.modifiedDate = modifiedDate;
      }
  
      public Child getChild() {
          return child;
      }
  
      public void setChild(Child child) {
          this.child = child;
      }
  }
  </code></pre>
  <pre><code>@Entity
  @Audited
  public class Child implements Serializable {
  
      @Id
      @GeneratedValue(generator = &quot;system-uuid&quot;)
      @GenericGenerator(name = &quot;system-uuid&quot;, strategy = &quot;uuid2&quot;)
      @Size(max = 36)
      @Column(length = 36)
      private String id;
  
      private String sampleField;
  
      public String getId() {
          return id;
      }
  
      public void setId(String id) {
          this.id = id;
      }
  
      public String getSampleField() {
          return sampleField;
      }
  
      public void setSampleField(String sampleField) {
          this.sampleField = sampleField;
      }
  }
  </code></pre>
  <p>Now I need to update the parents modified (date &amp; user) fields. If i change only the field (sampleField) in the child then the modified fields are not updated in the parent.</p>
  <p>I found following two statements and if I understand it correctly then its not a good idea to change the parent in lifecycle events (Interceptor, EventListener, Callbacks) of the child.</p>
  <p><a href="https://stackoverflow.com/a/7754856">Statement 1</a>
  <a href="https://stackoverflow.com/a/55323526">Statement 2</a></p>
  <p>What is a save way to address my problem?</p>
  <p><strong>Update</strong></p>
  <p>I found this way from vlad: <a href="https://vladmihalcea.com/how-to-increment-the-parent-entity-version-whenever-a-child-entity-gets-modified-with-jpa-and-hibernate/" rel="nofollow noreferrer">How to increment the parent entity version</a>. But I'm not sure if this way is safe. Life cycle events are also used.</p>