title: Hibernate: Foreign key has the wrong number of columns
body: <p>I have defined a many-to-many relationship between my two entity classes User and Permission. User has a primary key composite of username and countyId, and my Permission table has a regular integer Id. The table UserPermission has the three foreign keys as its primary key: username, countyId and permissionId.</p>
  
  <p>Since this is a legacy database, I won't have the opportunity to do the Right Thing(™) and make an integer primary key on User.</p>
  
  <p>I've defined the many-to-many relationship like this in User.class:</p>
  
  <pre><code>@ManyToMany(targetEntity=Permission.class, cascade={ CascadeType.PERSIST, CascadeType.MERGE } )
  @JoinTable(name="tblUserPermission",
  joinColumns = { @JoinColumn(name="username"), @JoinColumn(name="countyId") },
  inverseJoinColumns = { @JoinColumn(name="permissionId") })
  private Collection&lt;Permission&gt; permissions;
  </code></pre>
  
  <p>Permission.class says this:</p>
  
  <pre><code>@ManyToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "permissions", targetEntity = User.class )
  private Collection&lt;User&gt; users;
  </code></pre>
  
  <p>I thought this was the way to go, but when I fire up my Spring context that uses Hibernate 3, I get: </p>
  
  <pre><code>Caused by: org.hibernate.AnnotationException: A Foreign key refering com.mydomain.data.entities.User from com.mydomain.data.entities.Permission has the wrong number of column. should be 1
  </code></pre>
  
  <p>What have I done wrong in my annotation? It should be 2, not 1.</p>
  
  <hr>
  
  <p>Update:</p>
  
  <p>Arthur suggested I add referencedColumnName, but that gave me a new exception:</p>
  
  <pre><code>Caused by: org.hibernate.AnnotationException: referencedColumnNames(username, countyId) of com.mydomain.data.entities.Permission.permissions referencing com.mydomain.data.entities.User not mapped to a single property
  </code></pre>
  
  <p>On his request, here follow the code:
  Permission.Class:</p>
  
  <pre><code>package com.mydomain.data.entities;
  
  import java.io.Serializable;
  import java.util.Collection;
  import javax.persistence.*;
  import org.hibernate.annotations.ForeignKey;
  
  @Entity
  @Table(name = "tblPermission")
  public class Permission extends PublishableEntityImpl implements Serializable, Cloneable {
  
      private static final long serialVersionUID = 7155322069731920447L;
  
      @Id
      @Column(name = "PermissionId", length = 8, nullable = false)
      private String PermissionId = "";
  
      @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name = "CountyId", nullable = false)
      @ForeignKey(name="FK_CountyID")
      private County county;
  
      @Column(name = "Permission", nullable = true)
      private Integer permission = 1;
  
      @ManyToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE},
               mappedBy = "Permissions",
               targetEntity = Item.class )
      private Collection&lt;Item&gt; items;
  
      @ManyToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE},
               mappedBy = "Permissions",
               targetEntity = User.class )
      private Collection&lt;User&gt; users;
  
      /** Getters and Setters **/
  }
  </code></pre>
  
  <p>and User.class</p>
  
  <pre><code>package com.mydomain.data.entities;
  
  import java.util.*;
  import java.io.Serializable;
  import javax.persistence.*;
  import org.hibernate.annotations.ForeignKey;
  import org.hibernate.annotations.IndexColumn;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.GrantedAuthorityImpl;
  import org.springframework.security.core.userdetails.UserDetails;
  
  @Entity
  @Table(name = "tblUser")
  public class User extends PublishableEntityImpl implements Serializable, Cloneable {
  
      @Id
      @Column(name = "CountyId", nullable = false)
      private Integer countyId;
  
      @Id
      @Column(name = "Username", length = 25, nullable = false)
      private String username;
  
      @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name = "CountyId", nullable = false, insertable=false, updatable=false)
      @ForeignKey(name="FK_CountyID")
      private County county;
  
      @Column(name = "Name", length = 50, nullable = true)
      private String name;
  
      @Column(name = "Password", length = 30, nullable = true)
      private String password;
  
      @Column(name = "Role", nullable = false)
      private Integer role;
  
      @ManyToMany(targetEntity=Permission.class,
              cascade={ CascadeType.PERSIST, CascadeType.MERGE } )
      @JoinTable(name="tblUserPermission",
              joinColumns = { @JoinColumn(name="Username", referencedColumnName="Username"), @JoinColumn(name="CountyId", referencedColumnName="CountyId") },
              inverseJoinColumns = { @JoinColumn(name="PermissionId", referencedColumnName="PermissionId") })
     private Collection&lt;Permission&gt; permissions;
  
      @OneToMany(fetch=FetchType.LAZY, mappedBy="county")
      @IndexColumn(name="version")
      private List&lt;Version&gt; versions;
  
      /** Getters and setters **/
  }
  </code></pre>
  
  <p>Cheers</p>
  
  <p>Nik</p>