title: Override @CollectionTable reference to primary-key column
body: <h2>In short</h2>
  
  <p>JPA documentation says that <a href="https://docs.oracle.com/javaee/6/api/javax/persistence/CollectionTable.html#joinColumns()" rel="nofollow">CollectionTable.joinColumns</a> property is "The foreign key columns of the collection table which reference the primary table of the entity."</p>
  
  <p>Can I make it reference not the primary key column of the entity table, but another column which has unique values?</p>
  
  <h2>Details</h2>
  
  <p>There is a legacy database with following tables (simplified):</p>
  
  <pre><code>staff:
    user_id (int)
    user_name (varchar, primary key)
  
  staff_privileges:
    id_user (int)
    privileges_id (int) 
    privileges_status (boolean)
  </code></pre>
  
  <p>Now I'm introducing Hibernate into the project and trying to map this tables to the entity:</p>
  
  <pre><code>@Entity
  @Table(name = "staff")
  public class User {
  
    @Column(name = "user_id")
    private Integer userId;
  
    @Id
    @Column(name = "user_name")
    private String userName;    // primary key column (legacy, cannot be changed)
  
    @ElementCollection
    @CollectionTable(name = "staff_privileges", joinColumns = @JoinColumn(name = "id_user"))
    @MapKeyColumn(name = "privileges_id")
    @Column(name = "privileges_status")
    private Map&lt;Integer, Boolean&gt; privileges; // privilegeId &lt;-&gt; isEnabled
  }
  </code></pre>
  
  <p>Won't work because @CollectionTable creates reference to primary key: <code>staff_privileges.id_user -&gt; staff.user_name (primary key)</code>.
  It should be: <code>staff_privileges.id_user -&gt; staff.id_user</code>.
  Is there way to override this reference? Are there any Hibernate or JPA annotations for this? I would like to keep things simple and not introduce any new Entity or Embeddable if possible.</p>