title: HQL query for retrive rows from ManyToMany Join Table
body: <p>I'm working on website where <strong>user</strong> can <strong>subscribe</strong> to <strong>Organisation</strong>.
  when I'm going to implement Subscribe function and I face the following problem.</p>
  <p>In sort I want to create model class of ManyToMany join table for retrieve rows from table to check which Organisations are subscribe by user.
  And In Hibernate I can't create Table without primary key.but in join table one user can subscribe to many organisation and one organisation has many subscriber so primary key are repeat and I got exception <code>ERROR: Duplicate entry '1' for key 'PRIMARY'</code>.</p>
  <p><strong>hibernate.cfg.xml</strong> contain</p>
  <pre><code>&lt;mapping class=&quot;model.User&quot;/&gt;
  &lt;mapping class=&quot;model.Post&quot;/&gt;
  &lt;mapping class=&quot;model.UserSubscribes&quot;/&gt;
  </code></pre>
  <p><strong>User.java</strong></p>
  <pre><code>package model;
      
  @Entity
  @Table(name=&quot;user&quot;,
          uniqueConstraints = {@UniqueConstraint(columnNames={&quot;email&quot;})}
          )
  @org.hibernate.annotations.Entity(dynamicUpdate=true,selectBeforeUpdate=true)
  
  public class User implements Serializable {
      
      @Id 
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private long userId;//1
      private String email;//1
      private String password;//
      
      public User(long userId, String email, String password){
          this.userId = userId;
          this.email = email;
          this.password = password;
      }
      
      @ManyToMany(fetch = FetchType.LAZY)
      @JoinTable(
              name=&quot;UserSubscribes&quot;,
              joinColumns={ @JoinColumn(name=&quot;userId&quot;,referencedColumnName=&quot;userId&quot;) },
              inverseJoinColumns={ @JoinColumn(name=&quot;orgId&quot;, referencedColumnName=&quot;orgId&quot;) }
      )
      private Collection&lt;Organisation&gt; orgSubscribes = new ArrayList&lt;Organisation&gt;();
      
  
      //Getter &amp; Setter
      
  }
  </code></pre>
  <p><strong>Organisation.java</strong></p>
  <pre><code>package model;
  
  @Entity
  @Table(name=&quot;org&quot;,
          uniqueConstraints = {@UniqueConstraint(columnNames={&quot;email&quot;})}
          )
  @org.hibernate.annotations.Entity(dynamicUpdate=true,selectBeforeUpdate=true)
  
  public class Organisation implements Serializable {
  
      @Id 
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private long orgId;
      private String email;
      private String password;
      
      public Organisation(long orgId, String email, String password){
          this.orgId = orgId;
          this.email = email;
          this.password = password;
      }
      
      //Getter &amp; Setter
  }
  </code></pre>
  <p><strong>UserSubscribes.java</strong></p>
  <pre><code>package model;
  
  @Entity
  @Table(name=&quot;UserSubscribes&quot;)
  public class UserSubscribes implements Serializable {
      
      @Id 
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private long userId;
      private long orgId;
      
      //Getter &amp; Setter
  }
  </code></pre>
  <p>Subscribe.java</p>
  <pre><code>package view.action;
  
  public class Subscribe extends ActionSupport {
  
      public String execute(){
          
          Session session = HibernateUtill.getSessionFactory().getCurrentSession();
          session.beginTransaction();
          
          System.out.println(&quot;Subscribbbbbbbbbbbbbbbbbbbbbbbbbbbbb&quot;);
          
          User u1 = new User(1, &quot;ppp&quot;, &quot;ppp&quot;);
          User u2 = new User(2, &quot;qqq&quot;, &quot;qqq&quot;);
          Organisation o1 = new Organisation(1, &quot;ppp&quot;, &quot;ppp&quot;);
          Organisation o2 = new Organisation(2, &quot;qqq&quot;, &quot;qqq&quot;);
          Organisation o3 = new Organisation(3, &quot;www&quot;, &quot;www&quot;);
          Organisation o4 = new Organisation(4, &quot;eee&quot;, &quot;eee&quot;);
          
          session.save(o1);
          session.save(o2);
          session.save(o3);
          session.save(o4);
          session.save(u1);
          session.save(u2);
          
          u1.getOrgSubscribes().add(o1);
          u1.getOrgSubscribes().add(o2);
          u1.getOrgSubscribes().add(o3);
          
          session.saveOrUpdate(u1);
          
          session.getTransaction().commit();
          
          return SUCCESS;
      }
  }
  </code></pre>
  <p>and I got this output and error</p>
  <pre><code>Subscribbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  Hibernate: insert into org (email, password) values (?, ?)
  Hibernate: insert into org (email, password) values (?, ?)
  Hibernate: insert into org (email, password) values (?, ?)
  Hibernate: insert into org (email, password) values (?, ?)
  Hibernate: insert into user (email, password) values (?, ?)
  Hibernate: insert into user (email, password) values (?, ?)
  Hibernate: insert into UserSubscribes (userId, orgId) values (?, ?)
  Hibernate: insert into UserSubscribes (userId, orgId) values (?, ?)
  Apr 27, 2014 4:43:52 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  WARN: SQL Error: 1062, SQLState: 23000
  Apr 27, 2014 4:43:52 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  ERROR: Duplicate entry '1' for key 'PRIMARY'
  </code></pre>
  <p>If I remove <code>&lt;mapping class=&quot;model.UserSubscribes&quot;/&gt;</code> from hibernate.cfg.xml mapping then it works perfect as following output.</p>
  <pre><code>Subscribbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  Hibernate: insert into org (email, password) values (?, ?)
  Hibernate: insert into org (email, password) values (?, ?)
  Hibernate: insert into org (email, password) values (?, ?)
  Hibernate: insert into org (email, password) values (?, ?)
  Hibernate: insert into user (email, password) values (?, ?)
  Hibernate: insert into user (email, password) values (?, ?)
  Hibernate: insert into UserSubscribes (userId, orgId) values (?, ?)
  Hibernate: insert into UserSubscribes (userId, orgId) values (?, ?)
  Hibernate: insert into UserSubscribes (userId, orgId) values (?, ?)
  </code></pre>
  <p>and output is</p>
  <p><img src="https://i.stack.imgur.com/ray6T.png" alt="enter image description here" /></p>
  <p>but I can't retrieve rows(using HQL) from it without map this table in hibernate.cfg.xml file.<br>
  If any possible solution for this problem I'm really thankful to you.
  Thank you in advance.</p>