title: Bi-directional @OneToOne with ONLY CascadeType.REMOVE saves changes: that is not wanted
body: <p><strong>The problem:</strong>  </p>
  
  <p>I have two entities, <code>SomeEntity</code> and <code>SomeEntityInfo</code> in a bi-directional one-to-one relation with only <code>CascadeType.REMOVE</code> cascading set.</p>
  
  <p>If <code>SomeEntity.someEntityInfo</code> is changed, and <code>SomeEntity</code> is (already existing) saved -> there shouldn't happen a cascading database update to the <code>SomeEntityInfo</code> table / object.</p>
  
  <p>But instead, the related entity is updated too</p>
  
  <p><strong>edit / update</strong>
  In other words: I want the <code>SomeEntityInfo</code> to be "(somewhat-)immutable". It should be created when <code>SomeEntity</code> is created, but not updated / version checked - optimistic locking - if <code>SomeEntity</code> is re-saved.</p>
  
  <p><em>what i did so far</em></p>
  
  <ul>
  <li><p>Returning a copy of <code>SomeEntityInfo</code> in the getter of <code>SomeEntity</code> results in</p>
  
  <blockquote>
    <p>a new object was found through a relationship that was not marked cascade PERSIST [..]</p>
  </blockquote></li>
  <li><p>(desperately)annotating with</p>
  
  <pre><code>@OneToOne(cascade = { CascadeType.REMOVE })
  @JoinColumn(name = "someentityinfo_id", updatable = false, insertable = true)
  private SomeEntityInfo someEntityInfo;
  </code></pre>
  
  <p>is related to the ID of the foreign key, not to the data inside the referenced object</p></li>
  </ul>
  
  <p><strong>Example - DB schema (mysql db)</strong></p>
  
  <pre><code>    CREATE TABLE someentity (
        id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
        version INT(11) NULL DEFAULT NULL,
        someentityinfo_id INT(11) UNSIGNED NULL DEFAULT NULL,
        PRIMARY KEY (id)
      )
      COLLATE='utf8_general_ci'
      ENGINE=InnoDB;
  
      CREATE TABLE someentityinfo (
        id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
        version INT(11) NULL DEFAULT NULL,
        status varchar(255) DEFAULT NULL,
        PRIMARY KEY (id)
      )
      COLLATE='utf8_general_ci'
      ENGINE=InnoDB;
  
      ALTER TABLE someentity
        ADD INDEX FK_someentityinfo_id (someentityinfo_id);
  
      ALTER TABLE someentity
        ADD CONSTRAINT FK_someentityinfo_id FOREIGN KEY (someentityinfo_id) REFERENCES someentityinfo (id);
  </code></pre>
  
  <p>Entity Code</p>
  
  <p>SomeEntity</p>
  
  <pre><code>    import javax.persistence.CascadeType;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.OneToOne;
      import javax.persistence.Table;
      import javax.persistence.Version;
  
      @Entity
      @Table(name = "someentity")
      public class SomeEntity {
  
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          private Integer id;
  
          @Version
          private Integer version;
  
          @OneToOne(cascade = { CascadeType.REMOVE })
          @JoinColumn(name = "someentityinfo_id")
          private SomeEntityInfo someEntityInfo;
          [getter/setter]
      }
  </code></pre>
  
  <p>SomeEntityInfo</p>
  
  <pre><code>    import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.OneToOne;
      import javax.persistence.Table;
      import javax.persistence.Version;
  
      @Entity
      @Table(name = "someentityinfo")
      public class SomeEntityInfo {
  
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          private Integer id;
  
          @Version
          private Integer version;
  
          private String status;
  
          @OneToOne(mappedBy = "someEntityInfo")
          private SomeEntity someEntity;
          [getter/setter]
      }
  </code></pre>
  
  <p>Used test scenario</p>
  
  <pre><code>    // create and persist entity and its info object
      em.getTransaction().begin();
      SomeEntity se = new SomeEntity();
      SomeEntityInfo seInfo = new SomeEntityInfo();
      se.setSomeEntityInfo(seInfo);
      seInfo.setSomeEntity(se);
  
      seInfo.setStatus("status 1");
  
      em.persist(se);
      em.persist(se.getSomeEntityInfo());
      em.getTransaction().commit();
  
      // load created entity, modify its info and expect
      // to NOT update the info object while saving the entity again
      em.getTransaction().begin();
      SomeEntity loadedSe = em.find(SomeEntity.class, Integer.valueOf(se.getId()));
  
      loadedSe.getSomeEntityInfo().setStatus("do not cascade update");
  
      // as Chris said below, not necessary to explicit save managed entity again
      // em.persist(loadedSe);
  
      em.getTransaction().commit();
  </code></pre>
  
  <p><strong>Environment</strong></p>
  
  <p>EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd</p>
  
  <p><strong>Additional Information</strong></p>
  
  <p>The specification (<a href="http://wiki.eclipse.org/Introduction_to_EclipseLink_JPA_(ELUG)#.40OneToOne" rel="nofollow">http://wiki.eclipse.org/Introduction_to_EclipseLink_JPA_(ELUG)#.40OneToOne</a>) sais:</p>
  
  <blockquote>
    <p>cascade – By default, JPA does not cascade any persistence operations to the target of the association.</p>
  </blockquote>
  
  <p>that is not the case (changes are cascaded).. what am I missing here?</p>