title: JPA - Using insertable/updatable
body: <p>I am writing a webservice to maintain a database.  I am trying to use JPA (EclipseLink) for the entity classes.  However, the database uses natural primary keys and therefore there's potential that an update on the ID fields will fail due to foreign key constraints.  Our DBA has provided a function to update the ID fields which will create a new parent record with the updated ID, update the child records to point to the new parent and delete the old parent.</p>
  
  <p>If the ID fields could be updated "normally", I would have a situation like this:</p>
  
  <pre><code>@Entity
  @Table(name = "PARENT")
  public class Parent implements Serializable
  {
      private static final long serialVersionUID = 1L;
      private String parent;
      private String attribute;
      private Set&lt;Child&gt; childs;
  
      public Parent()
      {
      }
  
      @Id
      @Column(name = "PARENT")
      public String getParent()
      {
          return this.parent;
      }
  
      public void setParent(String parent)
      {
          this.parent = parent;
      }
  
      @Column(name = "ATTRIBUTE")
      public String getAttribute()
      {
          return this.attribute;
      }
  
      public void setAttribute(String attribute)
      {
          this.attribute = attribute;
      }
  
      @OneToMany(mappedBy = "parentBean")
      public Set&lt;Child&gt; getChilds()
      {
          return this.childs;
      }
  
      public void setChilds(Set&lt;Child&gt; childs)
      {
          this.childs = childs;
      }
  }
  
  @Entity
  @Table(name = "CHILD")
  public class Child implements Serializable
  {
      private static final long serialVersionUID = 1L;
      private String child;
      private String attribute;
      private Parent parentBean;
  
      public Child()
      {
      }
  
      @Id
      @Column(name = "CHILD")
      public String getChild()
      {
          return this.child;
      }
  
      public void setChild(String child)
      {
          this.child = child;
      }
  
      @Column(name = "ATTRIBUTE")
      public String getAttribute()
      {
          return this.attribute;
      }
  
      public void setAttribute(String attribute)
      {
          this.attribute = attribute;
      }
  
      @ManyToOne
      @JoinColumn(name = "PARENT")
      public Parent getParent()
      {
          return this.parent;
      }
  
      public void setParent(Parent parent)
      {
          this.parent = parent;
      }
  }
  </code></pre>
  
  <p>I also have a GenericServiceBean class with a method to call functions:</p>
  
  <pre><code>@Stateless
  public class GenericServiceBean implements GenericService
  {
      @PersistenceContext(unitName = "PersistenceUnit")
      EntityManager em;
  
      public GenericServiceBean()
      {
          // empty
      }
  
      @Override
      public &lt;T&gt; T create(T t)
      {
          em.persist(t);
          return t;
      }
  
      @Override
      public &lt;T&gt; void delete(T t)
      {
          t = em.merge(t);
          em.remove(t);
      }
  
      @Override
      public &lt;T&gt; T update(T t)
      {
          return em.merge(t);
      }
  
      @Override
      public &lt;T&gt; T find(Class&lt;T&gt; type, Object id)
      {
          return em.find(type, id);
      }
  
      . . . 
  
      @Override
      public String executeStoredFunctionWithNamedArguments(String functionName,
              LinkedHashMap&lt;String, String&gt; namedArguments)
      {
          Session session = JpaHelper.getEntityManager(em).getServerSession();
  
          StoredFunctionCall functionCall = new StoredFunctionCall();
          functionCall.setProcedureName(functionName);        
          functionCall.setResult("RESULT", String.class);
  
          for (String key : namedArguments.keySet())
          {
              functionCall.addNamedArgumentValue(key, namedArguments.get(key));
          }
  
          ValueReadQuery query = new ValueReadQuery();
          query.setCall(functionCall);
  
          String status = (String)session.executeQuery(query);
  
          return status;
      }
  }
  </code></pre>
  
  <p>If I set the ID fields to be not editable:</p>
  
  <pre><code>    @Id
      @Column(name = "PARENT", udpatable=false)
      public String getParent()
      {
          return this.parent;
      }
  </code></pre>
  
  <p>and call <code>parent.setParent(newParent)</code> will this still update the ID in the entity object?  How does this affect any child entities? Will they also be updated (or not)?</p>
  
  <p>Another scenario I don't know how to deal with is where I need to update both the ID and another attribute.  Should I call the function which updates (and commits) the ID in the database then make calls to set both the ID and attribute via the normal set* methods and then the persistence context will only commit the attribute change?</p>
  
  <p>Perhaps this is a situation where JPA is not appropriate?</p>
  
  <p>Any advice on this is greatly appreciated.</p>