title: How to Represent Composite keys in Hibernate using Annotations?
body: <p>So I reverse engineered some tables from my db and when I try to save my object to the db I get the following  error:</p>
  
  <p>Initial SessionFactory creation failed.org.hibernate.AnnotationException: A Foreign key refering com.mycode.Block from com.mycode.Account has the wrong number of column. should be 2
  Exception in thread "main" java.lang.ExceptionInInitializerError</p>
  
  <p>The Domain objects Are Block which contains a number of Account Objects:</p>
  
  <pre><code>    @OneToMany(fetch = FetchType.LAZY, mappedBy = "Block")
  public Set&lt;EAccount&gt; getAccounts() {
      return this.Accounts;
  }
  </code></pre>
  
  <p>Account has a Composite key of Id and Role. This has been setup in a seperate Class:</p>
  
  <pre><code>@Embeddable
  public class BlockAccountId implements java.io.Serializable {
  
  private long blockOid;
  private String accountRole;
  
  public BlockAccountId() {
  }
  
  public BlockAccountId(long blockOid, String accountRole) {
      this.blockOid = blockOid;
      this.accountRole = accountRole;
  }
  
  @Column(name = "BLOCK_OID", nullable = false)
  public long getBlockOid() {
      return this.blockOid;
  }
  
  public void setBlockOid(long blockOid) {
      this.blockOid = blockOid;
  }
  
  @Column(name = "ACCOUNT_ROLE", nullable = false, length = 10)
  public String getAccountRole() {
      return this.accountRole;
  }
  
  public void setAccountRole(String accountRole) {
      this.accountRole = accountRole;
  }
  </code></pre>
  
  <p>So I want to know. How can I Link the tables Block and account on blockOid but still ensure the account table has both blockOid and accountRole as a composite key. </p>
  
  <p>Any examples would be greatly appreciated!</p>
  
  <p>N.B this is a Block (One) to Account (Many) relationship.</p>
  
  <p>Thanks</p>