title: @JoinColumns with composite primary key on both tables
body: <p>I have to map a legacy db with a couple of tables like these</p>
  
  <pre><code>---------
  - GROUP -
  ---------
  +idcust -
  +key    -
  +idgroup- 
  -domain -
  ---------
  
  pk(idcust,key,idgroup)
  
  ------------------
  -   GROUP_LABEL  -
  ------------------
  +idcust          -
  +key             -
  +idgroup         - 
  +idlabel         -
  -domain          -
  -description_lang-
  ------------------
  
  pk(idcust,key,idgroup,idlabel)
  fk(idcust,key,idgroup) references group.pk
  </code></pre>
  
  <p>Both of them have composite primary keys, and the latter one's has all the fields of the first one. I cannot change the tables, so I'm stuck with mapping them as they are.</p>
  
  <p>Here are the classes:</p>
  
  <p>This is for the one side of the relationship</p>
  
  <pre><code>@Embeddable
  public class GroupId implements Serializable {
  
    private static final long serialVersionUID = 7202018350067509393L;
  
    @Column(name = "idcust")
    private String idCust;
    @Column(name = "key")
    private String key;
    @Column(name = "idgroup")
    private Long idGroup;
  
    //getters, setters, hashcode, equals
  
  }
  
  @Entity
  @Table(name = "group")
  public class Group{
  
    @EmbeddedId
    private GroupId groupId;
  
    @Column(name = "domain")
    private String domain;
  
    @OneToMany(mappedBy="group", fetch=FetchType.EAGER)
    private Set&lt;GroupLabels&gt; groupLabels;
  
    //getters, setters
  
  }
  </code></pre>
  
  <p>While this is for the many side</p>
  
  <pre><code>@Entity
  @Table(name = "group_labels")
  public class GroupLabels{
  
      @EmbeddedId
      private GroupLabelsId groupLabelsId;
  
      @Column(name = "description_lang")
      private String descriptionLang;
  
      @Column(name = "domain")
      private String domain;
  
      @ManyToOne
      @JoinColumns({
          @JoinColumn(name="idcust", referencedColumnName="idcust" ,insertable=false, updatable=false),
          @JoinColumn(name="key", referencedColumnName="key" ,insertable=false, updatable=false),
          @JoinColumn(name="idgroup", referencedColumnName="idgroup" ,insertable=false, updatable=false)
      })
      private Group group;
  
  //getters, setters
  
  }   
  
  
  @Embeddable
  public class GroupLabelsId implements Serializable {
  
      private static final long serialVersionUID = 761043173785225772L;
  
      @Column(name = "idcust")
      private String idCust;
      @Column(name = "key")
      private String key;
      @Column(name = "idgroup")
      private Integer idGroup;
      @Column(name = "idlabel")
      private Integer idLabel;
  
  //getters, setters, hashcode, equals
  
  }
  </code></pre>
  
  <p>This mapping kind of works, as I am able to retrieve the entities, however, I took a look at the sql generated by this simple test:</p>
  
  <pre><code>@Test
  public void testIntegrity(){
      Group got = (Group) sessionFactory.getCurrentSession().createCriteria(Group.class)
              .add(Restrictions.idEq(groupSaved.getGroupId())).uniqueResult();
      assertEquals(groupSaved.getGroupId(),got.getGroupId());
  
  }
  </code></pre>
  
  <p>This is the sql code:</p>
  
  <pre><code>select
          this_.idcust as idcust1_0_1_,
          this_.idgroup as idgroup2_0_1_,
          this_.key as key3_0_1_,
          this_.domain as domain4_0_1_,
          grouplabel2_.idcust as idcust1_0_3_,
          grouplabel2_.idgroup as idgroup2_0_3_,
          grouplabel2_.key as key4_0_3_,
          grouplabel2_.idcust as idcust1_1_3_,
          grouplabel2_.idgroup as idgroup2_1_3_,
          grouplabel2_.idlabel as idlabel3_1_3_,
          grouplabel2_.key as key4_1_3_,
          grouplabel2_.idcust as idcust1_1_0_,
          grouplabel2_.idgroup as idgroup2_1_0_,
          grouplabel2_.idlabel as idlabel3_1_0_,
          grouplabel2_.key as key4_1_0_,
          grouplabel2_.description_lang as descript5_1_0_,
          grouplabel2_.domain as domain6_1_0_ 
      from
          group this_ 
      left outer join
          group_labels grouplabel2_ 
              on this_.idcust=grouplabel2_.idcust 
              and this_.idgroup=grouplabel2_.idgroup 
              and this_.key=grouplabel2_.key 
      where
          this_.idcust=? 
          and this_.key=? 
          and this_.domain=?
  </code></pre>
  
  <p>As you see it retrieves multiple times the ids of the second entity, and this makes me believe my mapping is sub optimal.</p>
  
  <p>I'm using Hibernate 4.3.10 as ORM.</p>
  
  <p>Is there a way I can correct the @JoinColumns to avoid multiple columns retrieve?</p>