title: JPA doesn't t allow entity made of columns from multiple tables?
body: <p>I know this makes none sense as many tutorials state that you can use SecondaryTable annotation, however it doesn't work in hibernate. I have schema like this:</p>
  
  <pre><code>@Entity
  @Table(name="server")
  @SecondaryTable(name="cluster", pkJoinColumns = { @PrimaryKeyJoinColumn(name = "uuid", referencedColumnName = "cluster_uuid") })
  public class Server {
      @Id
      @Column(name = "uuid")
      private String uuid;
  
      @Column(name = "cluster_uuid")
      private String clusterUuid;
  
        @Column(name = "ip", table="cluster")
        private String ip;
  ..... }
  
  @Entity
  @Table(name = "cluster")
  public class Cluster {
      @Id
      @Column(name = "uuid")
      private String uuid;
  
      @Column(name = "ip")
      private String ip;
  
  .....
  }
  </code></pre>
  
  <p>Server.clusterUuid is a foreign key to Cluster.uuid. <strong>I am hoping to get Server entity that fetches ip column from Cluster by joining Server.clusterUuid to Cluster.uuid.</strong></p>
  
  <p>Then I was greeted by a hibernate exception:</p>
  
  <blockquote>
    <p>Caused by: org.hibernate.AnnotationException: SecondaryTable
    JoinColumn cannot reference a non primary key
        at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:402)
        at org.hibernate.cfg.annotations.EntityBinder.bindJoinToPersistentClass(EntityBinder.java:620)
        at org.hibernate.cfg.annotations.EntityBinder.createPrimaryColumnsToSecondaryTable(EntityBinder.java:612)</p>
  </blockquote>
  
  <p>I see lots of people encountered this problem. But the first bug for this in Hibernate's bugzilla was 2010, I am surprised it's been there for over two years as this is supposed to be a basic feature. There is some post saying JPA spec only allows primary key to do the mapping, however, I get below from JPA wikibook</p>
  
  <blockquote>
    <p>JPA allows multiple tables to be assigned to a single class. The
    @SecondaryTable and SecondaryTables annotations or 
    elements can be used. By default the @Id column(s) are assumed to be
    in both tables, such that the secondary table's @Id column(s) are the
    primary key of the table <strong>and a foreign key to the first table</strong>. If
    the first table's @Id column(s) are not named the same the
    @PrimaryKeyJoinColumn or  can be used to
    define the foreign key join condition.</p>
  </blockquote>
  
  <p>it's obviously OK for non-primary key. Then I am confused why Hibernate didn't fix this problem as it seems to be easy to implement by a join clause.</p>
  
  <p>anybody knows how to overcome this problem? thank you.</p>