title: Hibernate Annotations : Many-to-Many relationship using Join table having common attribute in PK
body: <p>Is it possible to define a many to many relationship between two tables using a join table which has composite key, having a common attribute between the two tables?</p>
  
  <p>Let's say, we have a table structure like below and am trying to define association between kit and business_product using kit_business_product table. Here are the table definitions (simplified for this question).</p>
  
  <p>Table 1 : kit </p>
  
  <p>PK1 : kit_id, business_id</p>
  
  <p>Table 2 (Join Table) : kit_business_product</p>
  
  <p>PK2 : kit_id, business_id, manufacturer_id, product_id, name</p>
  
  <p>Table 3 : business_product</p>
  
  <p>PK3 : business_id, manufacturer_id, product_id, name</p>
  
  <p>Here are the java classes for these tables (simplified for this question).</p>
  
  <p>Kit.java</p>
  
  <pre><code>@Entity
  @Table (name= "kit")
  public class Kit
  {
      @EmbeddedId
      private KitId id;
  
      @ManyToMany
      @JoinTable(name = "kit_business_product",
      joinColumns = {@JoinColumn(name = "kit_id", referencedColumnName = "kit_id", nullable = false, insertable = false, updatable = false),
          @JoinColumn(name = "business_id", referencedColumnName = "business_id", nullable = false, insertable = false, updatable = false)},
      inverseJoinColumns = {
          @JoinColumn(name = "business_id", referencedColumnName = "business_id", nullable = false),
          @JoinColumn(name = "manufacturer_id", referencedColumnName = "manufacturer_id", nullable = false),
          @JoinColumn(name = "product_id", referencedColumnName = "product_id", nullable = false),
          @JoinColumn(name = "name", referencedColumnName = "name", nullable = false)
      })
      private Set&lt;BusinessProduct&gt; businessProducts = new HashSet&lt;BusinessProduct&gt;();
  }
  </code></pre>
  
  <p>KitId.java</p>
  
  <pre><code>@Embeddable
  public class KitId
  {
      @Column (name = "kit_id")
      private String kitId;
  
      @Column (name = "business_id")
      private long businessId;
  }
  </code></pre>
  
  <p>BusinessProduct.java</p>
  
  <pre><code>@Entity
  @Table(name = "business_product")
  public class BusinessProduct
  {
      @EmbeddedId
      private BusinessProductId id;
  }
  </code></pre>
  
  <p>BusinessProductId.java</p>
  
  <pre><code>@Embeddable
  public class BusinessProductId
  {
      @Column(name = "business_id")
      private long businessId;
  
      @Column(name = "manufacturer_id")
      private long  manufacturerId;
  
      @Column(name = "product_id")
      private String productId;
  
      @Column(name = "name")
      private String name;
  }
  </code></pre>
  
  <p>With this code, I get the below given error.</p>
  
  <pre><code>[RMI TCP Connection(3)-127.0.0.1] 2017-03-17 14:17:54,767 ERROR com.common.DatabaseSession [] Repeated column in mapping for collection: com.hibernate.Kit.businessProducts column: business_id
  org.hibernate.MappingException: Repeated column in mapping for collection: com.hibernate.Kit.businessProducts column: business_id
  at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:341) ~[hibernate3.jar:3.6.10.Final]
  at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:364) ~[hibernate3.jar:3.6.10.Final]
  </code></pre>
  
  <p>What would be the correct annotation for such a scenario? Any insight would be greatly appreciated. Thanks!</p>