title: Join 3 tables Hibernate & JPA
body: <p>I have three tables and I want to join them and it's driving me crazy.</p>
  
  <p>Tabels : <strong>Task</strong>
           <strong>Project</strong>
           <strong>User</strong></p>
  
  <p>They All have a <strong>Many-To-Many</strong> relationShip between them : like project has multiple users and vice versa and also Tasks have multiple users and vice versa and the same for project and users.</p>
  
  <p>I'm trying to find a way to implement that in JPA &amp; Hibernate and I have 2 choices : 
  ** 1 ** Join the 3 entities like this :</p>
  
  <p><strong><em>Project</em></strong> :</p>
  
  <pre><code>   @ManyToMany(fetch = FetchType.EAGER, 
      cascade =
      {
              CascadeType.DETACH,
              CascadeType.MERGE,
              CascadeType.REFRESH,
              CascadeType.PERSIST
      },
      targetEntity = User.class)
    @JoinTable(name = "Collaborators", 
             joinColumns = @JoinColumn(name = "ProjectFk", referencedColumnName = "id", updatable=false, nullable=false), 
             inverseJoinColumns = @JoinColumn(name = "UserFk", referencedColumnName = "id", updatable=false, nullable=false))
    private Set&lt;User&gt; users = new HashSet&lt;&gt;();
  </code></pre>
  
  <p><strong><em>User</em></strong> :</p>
  
  <pre><code>    @ManyToMany(fetch = FetchType.EAGER, 
              cascade =
                  {
                          CascadeType.DETACH,
                          CascadeType.MERGE,
                          CascadeType.REFRESH,
                          CascadeType.PERSIST
                  },
                  targetEntity = Project.class)
      @JoinTable(name = "Collaborators", 
             joinColumns = @JoinColumn(name = "UserFk", referencedColumnName = "id", updatable=false, nullable=false), 
             inverseJoinColumns = @JoinColumn(name = "ProjectFk", referencedColumnName = "id", updatable=false, nullable=false))
      private Set&lt;Project&gt; projects = new HashSet&lt;&gt;();
  </code></pre>
  
  <p><strong><em>Task</em></strong> :</p>
  
  <pre><code>    @ManyToMany(fetch = FetchType.EAGER, 
          cascade =
          {
                  CascadeType.DETACH,
                  CascadeType.MERGE,
                  CascadeType.REFRESH,
                  CascadeType.PERSIST
          },
          targetEntity = User.class)
      @JoinTable(name = "Collaborators", 
         joinColumns = @JoinColumn(name = "TaskFk", referencedColumnName = "id", updatable=false, nullable=false), 
         inverseJoinColumns = @JoinColumn(name = "ProjectFk", referencedColumnName = "id", updatable=false, nullable=false))
      @MapKeyJoinColumn(name = "User_id")
      @ElementCollection
      private Map&lt;Project, User&gt; projectUserMap = new HashMap&lt;&gt;();
  </code></pre>
  
  <p>But here it dosn't seem to work</p>
  
  <p><strong>2</strong> : Create a 4th Entity and name it <strong>Collaborator</strong> and then try to find a way to implement this SQL query : </p>
  
  <pre><code>select * from (
  select user.id, Project.id, Task.id
  inner join Project on user.join_key = Project.join_key
  inner join  Task  on task.join_key = = Project.join_key
  ) collaborator
  </code></pre>
  
  <p>Any help would be <strong>MUCH</strong> apreciated. Thanks.</p>