title: Hibernate @EmbeddedId + join
body: <p>I have a hibernate mapping problem. I have the following two DB tables (I am not allowed to change the DB):</p>
  <pre><code>LOCATIONS {
     ID, -- PK
     NAME
  }
  
  LOCATION_GROUPS {
     LOC_ID, -- PK, and FK to LOCATIONS.ID
     GROUP_NAME -- PK
  }
  </code></pre>
  <p>I tried to create entities for these DB tables, but i don't know how to map the connection between the tables. Here is my attempt (but it's wrong):</p>
  <p><strong>Embeddable</strong> Class</p>
  <pre><code>    @Embeddable
      public class LocationGroupId implements Serializable {
  
          private static final long serialVersionUID = -6437671620548733621L;
          private Location loc;  
          private String group;   
          
          @Column(name = &quot;LOC_ID&quot;)
          public Location getLoc() {
              return loc;
          }
          
          @Column(name = &quot;GROUP_NAME&quot;)
          public String getGroup() {
              return group;
          }
          
          // ...
      }   
  </code></pre>
  <p><strong>EmbeddedId used</strong></p>
  <pre><code>    @Entity
      @Table(name = &quot;LOCATION_GROUPS&quot;)
      public class LocationGroup {
  
          private LocationGroupId id;
  
          @EmbeddedId
          public LocationGroupId getId() {
              return id;
          }
          
          // ...
      }
  </code></pre>
  <pre><code>    @Entity
      @Table(name = &quot;LOCATIONS&quot;)
      public class Location {
  
          private Long id;
          private String name;
          private List&lt;LocationGroup&gt; groups;
          
          @Column(name = &quot;NAME&quot;)
          public String getName() {
              return this.name;
          }
          
          @OneToMany(mappedBy = &quot;id.loc&quot;)
          public List&lt;LocationGroup&gt; getGroups() {
              return this.groups;
          }
          
          @Id
          @Column(name = &quot;ID&quot;)
          @SequenceGenerator(name = &quot;LocationIdGen&quot;, sequenceName = &quot;LOCATION_SQ&quot;)
          @GeneratedValue(strategy = GenerationType.AUTO, generator = &quot;LocationIdGen&quot;)
          public Long getId() {
              return this.id;
          }
          
          // ...
      }
  </code></pre>
  <p>The difficulty is that I want to make an OneToMany connection between a column and a part of an embeddedId column.
  Any idea to this problem? (I'm using hibernate 4.0.1)</p>