title: How can I remove an object with a foreign primary key in JPA?
body: <p>I was always told that in case of a bidirectional relationship that you remove your entity as follows:</p>
  
  <ol>
  <li>Breakdown relationships</li>
  <li>Update owner entity</li>
  <li>Remove entities </li>
  </ol>
  
  <p>But now my primary key is part of the relationship, so this isn't possible anymore.</p>
  
  <p>Does anyone have an idea about how to remove the entity with the primary foreign key in this case?</p>
  
  <p>I have tried to set the "mappedby" property to null and than just call manager.remove(...), but than there's always another entity-manager that still has a managed instance of the removed entity, so i get the following error: </p>
  
  <pre><code>During synchronization a new object was found through a relationship
  that was not marked cascade PERSIST
  </code></pre>
  
  <p>Below i have put the code of the classes i'm using. </p>
  
  <p>The class<code>OrderBill</code> here has the primary foreign key pointing to <code>OrderWeek</code>. This primary key is also a composite key sadly making the code a bit less readable, but it shouldn't have any effect on the issue i'm having.</p>
  
  <pre><code>public class OrderBill{
          @EmbeddedId
          private OrderWeekPK orderWeekPK;
  
          @OneToOne
          @JoinColumns(value = {
              @JoinColumn(name="weekNr", referencedColumnName = "weekNr"),
              @JoinColumn(name="yearNr", referencedColumnName = "yearNr")})
          @MapsId
          private OrderWeek orderWeek;
      }
  
  public class OrderWeek{
      @OneToOne(mappedBy="orderWeek")
      private OrderBill orderBill;
  
      @EmbeddedId
      private OrderWeekPK orderWeekPK;
  }
  
  @Embeddable
  public class OrderWeekPK implements Serializable{
      @Column(name="yearNr")
      private int yearNr;
      @Column(name="weekNr")
      private int weekNr;
  }
  </code></pre>
  
  <p>Is there some strategy for this that I am missing?</p>
  
  <p><strong>Update</strong>: for now i can circumvent the issue by switching the owning and owned side (transferring the mappedby to the <code>OrderBill</code> class). Now i can break the relationship without touching the foreign primary key.</p>
  
  <p>This "solution" is still less than ideal: <code>OrderWeek</code> now has a redundant column (weekNr2 and yearNr2 for example) that is (or should be) identical to the primary key</p>
  
  <p>Maybe this helps people with the same problem until an actual solution is found.</p>