title: JPA2/EclipseLink OneToMany cascade persist issue "Referential integrity constraint violation"
body: <p>I have two entities A and B:</p>
  
  <p>A.java:</p>
  
  <pre><code>...
  public class A implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "IDA", nullable = false)
  private Integer ida;
  @Basic(optional = false)
  @Column(name = "NAME", nullable = false, length = 30)
  private String name;
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "a")
  private List&lt;B&gt; bList=new ArrayList();
  
  public void addB(B bp){
  bp.setA(this);
  bList.add(bp);
  }
  ...
  </code></pre>
  
  <p>B.java:</p>
  
  <pre><code>...
  public class B implements Serializable {
  private static final long serialVersionUID = 1L;
  @EmbeddedId
  protected BPK bPK;
  @Basic(optional = false)
  @Column(name = "NAME", nullable = false, length = 30)
  private String name;
  @JoinColumn(name = "A_IDA", referencedColumnName = "IDA", nullable = false, insertable = false, updatable = false)
  @ManyToOne(optional = false)
  private A a;
  ...
  </code></pre>
  
  <p>the <code>bPK</code> field is a Composite Primary Key:</p>
  
  <pre><code>@Embeddable
  public class BPK implements Serializable {
  @Basic(optional = false)
  @Column(name = "IDB", nullable = false, length = 20)
  private String idb;
  @Basic(optional = false)
  @Column(name = "A_IDA", nullable = false)
  private int aIda;
  
  public BPK() {
  }
  
  public BPK(String idb, int aIda) {
      this.idb = idb;
      this.aIda = aIda;
  }
  ...
  </code></pre>
  
  <p>SQL code:</p>
  
  <pre><code>CREATE TABLE A (
  idA INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(30) NOT NULL,
  PRIMARY KEY(idA)
  );
  
  CREATE TABLE B (
  idB VARCHAR(20) NOT NULL,
  A_idA INTEGER UNSIGNED NOT NULL,
  name VARCHAR(30) NOT NULL,
  PRIMARY KEY(idB, A_idA),
  FOREIGN KEY(A_idA)
  REFERENCES A(idA)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
  );
  </code></pre>
  
  <p>the main code:</p>
  
  <pre><code>   A a=new A(null,"A1");
     BPK bpk=new BPK();
     bpk.setIdb("b1");
     a.addB(new B(bpk,"B1"));
  
     EntityManager em=getEntityManager();
     em.getTransaction().begin();
     em.persist(a);
     em.getTransaction().commit();
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code> Exception in thread "main" javax.persistence.RollbackException: Exception  [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504):    org.eclipse.persistence.exceptions.DatabaseException
   [EL Warning]: 2012-03-26 01:29:16.724--UnitOfWork(1902320872)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504):  org.eclipse.persistence.exceptions.DatabaseException
   Internal Exception: org.h2.jdbc.JdbcSQLException: Referential integrity constraint  violation: "CONSTRAINT_42_1: PUBLIC.B FOREIGN KEY(A_IDA) REFERENCES PUBLIC.A(IDA)"; SQL  statement:
   Internal Exception: org.h2.jdbc.JdbcSQLException: Referential integrity constraint   violation: "CONSTRAINT_42_1: PUBLIC.B FOREIGN KEY(A_IDA) REFERENCES PUBLIC.A(IDA)";...
  </code></pre>
  
  <p>the error indicates a violation of integrity constrains, but why?
  a single possibility whether the insertion of the entity B is made before A...
  Any help please?
  Thank you in advance.</p>
  
  <p><strong>EDIT</strong>:
  solved just replace this :</p>
  
  <pre><code> @JoinColumn(name = "A_IDA", referencedColumnName = "IDA", nullable = false, insertable = false, updatable = false)
  </code></pre>
  
  <p>by this one:</p>
  
  <pre><code> @MapsId("aIda")
  </code></pre>
  
  <p>finally B.java:</p>
  
  <pre><code> @NamedQuery(name = "B.findByName", query = "SELECT b FROM B b WHERE b.name = :name")})
   public class B implements Serializable {
   private static final long serialVersionUID = 1L;
   @EmbeddedId
   protected BPK bPK;
   @Basic(optional = false)
   @Column(name = "NAME", nullable = false, length = 30)
   private String name;
   @MapsId("aIda")
   @ManyToOne(optional = false)
   private A a;
  </code></pre>