title: Disable Cascade in ManyToOne relationship JPA when saving
body: <p>I'm having problems when saving entities in my DB. </p>
  
  <p>I have something like (very simplified) :</p>
  
  <pre><code>@Entity
  public class Building {
  
      @OneToMany(mappedBy = "building", fetch = FetchType.EAGER)
      private List&lt;Employee&gt; employees;
  
  }  
  
  @Entity
  public class Employee {
  
      @NotNull
      @ManyToOne
      @JoinFetch(INNER)
      @JoinColumn
      private Building building;
  
      @PostLoad
      private void onLoad(){
            if (this.plannedOrder == null) {
              //For old entities in this DB, update plannedOrder if null
              if (this.order &lt; FIRST.getCode()) {
                  this.plannedOrder = FIRST;
              } else if (this.order &gt;= FIRST.getCode() &amp;&amp; this.order &lt; SECOND.getCode()) {
                  this.plannedOrder = SECOND;
              } else if (this.order &gt;= DEFAULT.getCode() &amp;&amp; this.order &lt; SEC_LAST.getCode()) {
                  this.plannedOrder = DEFAULT;
              } else if (this.order &gt;= SEC_LAST.getCode() &amp;&amp; this.order &lt; LAST.getCode()) {
                  this.plannedOrder = SEC_LAST;
              } else if (this.order &gt;= LAST.getCode()) {
                  this.plannedOrder = LAST;
              } else {
                  this.plannedOrder = DEFAULT;
              }
      }
  
  }
  </code></pre>
  
  <p>The problem is when I save a Employee entity. In the Building entity you will have all employees modified because the @PostLoad annotation, so JPA will try to update these entities. However, I only want to update the Employee entity.</p>
  
  <p>Is some way to do this without changing the relations in my model? Could be a solution to remove the onLoad function?</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDITED:</strong></p>
  
  <p>Added the PostLoad code of PostLoad. This is a fix executed for old entities in DB without this field to be updated when saved. </p>
  
  <p><strong>EDITED 2</strong></p>
  
  <p>And this is how I'm saving my entity Employee with my EntityManager (if is a new Object, persist, else update it)</p>
  
  <pre><code>    if (entity.getId() == null) {
          entityManager.persist(entity);
          return entity;
      }
      return entityManager.merge(entity);        
  </code></pre>