title: Help Mapping a Composite Foreign Key in JPA 2.0
body: <p>I'm new to JPA and I'm trying to map a legacy database.  The files load correctly individually but the relationships are not working correctly.  Any help would be appreciated.</p>
  
  <p><strong>Java</strong></p>
  
  <pre><code>@Entity
  @IdClass(ParentKey.class)
  public class Parent {
      @Id
      @Column(name="code")
      private String code;
  
      @Id
      @Column(name="id")
      private int id;
  
      @OneToMany(mappedBy="parent")
      private List&lt;Child&gt; children = new ArrayList&lt;Child&gt;();
  }
  
  public class ParentKey {
      private String code;
      private int id;
  }
  
  @Entity
  @IdClass(ChildKey.class)
  public class Child {
      @Id
      @JoinColumns({
          @JoinColumn(name="code")
          @JoinColumn(name="id")
      })
      private Parent parent;
  
      @Id
      @Column(name="index")
      private int index;
  }
  
  public class ChildKey {
      private String code;
      private int id;
      private int index;
  }
  </code></pre>
  
  <p><strong>SQL</strong></p>
  
  <pre><code>create table Parent(
    code char(4) not null,
    id int not null,
    primary key(code,id)
  );
  
  create table Child(
    code char(4) not null,
    id int not null,
    index int not null,
    primary key(code, id, index),
    foreign key(code, id) references Parent(code,id)
  );
  </code></pre>
  
  <p>edit 1:
  add the ChildKey and ParentKey classes.</p>