title: JPQL and Join Table
body: <p>My understanding of SQL and JPQL are not that great and I have been trying to create a JPQL query of the following sql statement:</p>
  
  <pre><code>select group.* from user, user_group, group 
  where user_group.user_id = user.id 
  and user_group.group_id = group.id 
  and user.id = [userID to search]
  </code></pre>
  
  <p>edit: Woops I forgot to add the search by user id part to the query. I would like to get all groups that a user belongs in.</p>
  
  <p>But I just cannot get the syntax correct. Any help would be greatly appreciated.</p>
  
  <p>Relevant code snippets:</p>
  
  <p>Group.java</p>
  
  <pre><code>@Table(name = "group")
  @Entity
  public class Group implements Serializable {
  
  @Id
  @GeneratedValue
  @Column(name = "id")
  private Integer id;
  
  @JoinTable(name = "user_group", joinColumns = {
      @JoinColumn(name = "group_id", referencedColumnName = "id")}, inverseJoinColumns = {
      @JoinColumn(name = "user_id", referencedColumnName = "id")})
  @ManyToMany
  private Collection&lt;User&gt; userCollection;
  
  }
  </code></pre>
  
  <p>User.java</p>
  
  <pre><code>@Table(name = "user")
  @Entity
  public class User implements Serializable {
  
  @Id
  @NotNull
  @GeneratedValue
  @Column(name = "id")
  private Integer id;
  
  @Column(name = "email", unique=true, nullable=false)
  private String email;
  
  @ManyToMany(mappedBy = "userCollection")
  private Collection&lt;Group&gt; GroupCollection;
  }
  </code></pre>