title: JPA: how to use @ElementCollection annotation?
body: <p>I need your help to map the relation in Hibernate of two tables using
  the @ElementCollection annotation.</p>
  
  <p>The first one is the parent table<br>
  Table name: <strong>Parent</strong><br>
  DB Columns</p>
  
  <pre><code>KEY1         Char      (first primary key field)
  KEY2         Char      (second primary key field)
  DESCRIPTION  Char
  DEPENDENTID  BigInt
  </code></pre>
  
  <p>The second one is the dependent table<br>
  TableName: <strong>Dependent</strong><br>
  DB Columns</p>
  
  <pre><code>PARENTID     BigInt    (first primary key field)
  CODE         Char      (second primary key field)
  FIELD1       Char
  FIELD2       Char
  </code></pre>
  
  <p>I need to define the PK for both tables using @EmbeddedId annotation,
  so I have created the two classes:</p>
  
  <pre><code>@Embeddable
  public class ParentPK implements Serializable
  {
     @Column(name="K1")
     private String iK1;
     @Column(name="K2")
     private String iK2;
    // I omit the constructor, getter, setter, equals, hashcode method 
  }
  
  @Embeddable
  public class DependentPK implements Serializable
  {
    @Column(name="PARENTID")
    private String iParentId;
    @Column(name="CODE")
    private String iCode;
    // I omit the constructor, getter, setter, equals, hashcode method 
  }
  </code></pre>
  
  <p>and then I've created the two beans:<br>
  The class for the DEPENDENT table.<br>
  Note that in this class I do not want to have any relational annotation</p>
  
  <pre><code>@Entity
  @Table(name = "DEPENDENT")
  public class DependentBean implements Serializable
  {
     @EmbeddedId
     private DependentPK iDependentPK;
     @Column(name = "FIELD1")
     private String iField1;
     @Column(name = "FIELD2")
     private String iField2;
     // I omit the constructor, getter, setter methods
  }
  </code></pre>
  
  <p>And the class for the PARENT table</p>
  
  <pre><code>@Entity
  @Table(name = "PARENT")
  public class ParentBean implements Serializable
  {
     @EmbeddedId
     ParentPK iParentPK;
     @Column(name = "DESCRIPTION")
     private String iDescription;
     @Column(name = "DEPENDENTID")
     private long iDependentId;
     @ElementCollection
     @CollectionTable(name="DEPENDENT", joinColumns={@JoinColumn(name="PARENTID",  referencedColumnName="DEPENDENTID")})
     private Set&lt;DependentBean&gt; iDependentBeans = new HashSet&lt;DependentBean&gt;();
     // I omit the constructor, getter, setter methods
  }
  </code></pre>
  
  <p>When I try to deploy I got the error:<br></p>
  
  <blockquote>
    <p>Caused by: org.hibernate.MappingException: Foreign key
    (FK9619C2A17B05CB2:DEPENDENT
    [iDependentBeans_PARENTID,iDependentBeans_CODE])) must have same
    number of columns as the referenced primary key (DEPENDENT
    [PARENTID,iDependentBeans_PARENTID,iDependentBeans_CODE])</p>
  </blockquote>
  
  <p>So I'm doing something wrong, but I can not imagine what.
  Could anyone wants to help me, please?</p>