title: Hibernate JPA, joining multiple tables
body: <p>I have a very interesting question: How do i join in hibernate if i have 3 tables? Example: Having table A, B, C;</p>
  
  <pre><code> @Entity
   public class A {
        private String name;
        private Int idA;
        ...
   } 
  
   @Entity
   public class B {
        private String lastName;
        private Int idB;
        ...
   }
  
   @Entity
   public class C {
        private String name;
        private String lastName;
        ...
   }
  </code></pre>
  
  <p>and my native query will look like: "select * from a inner join b inner join c on a.idA = b.idB and b.lastName = c.lastName and a.name = c.name"</p>
  
  <p>in hibernate you can use @JoinColum if you wanna join 2 tables, and @JoinTable, if you are joining table A with table C via table B. </p>
  
  <p>so in HQL query will look like: "from A a inner join a.b as b inner join a.c", where </p>
  
  <pre><code>@Entity
  public class A {
  ...
  @OneToMany
  @JoinColumn(name="idB", referencedColumnName="idA")
  private List&lt;B&gt; b;
  ...
  
  @ManyToMany
  @JoinTable(name = "B",
       joinColumns = {@JoinColumn(name="idB", referencedColumnName="ioA")},
       inverseJoinColumns = {@JoinColumn(name="lastName",referencedColumnName="lastName")}
   )
  private List&lt;C&gt; c;
  ...
  }
  </code></pre>
  
  <p>but still in this case i dont have a direct access from table A to table C, can anyone explain me how would i get</p>
  
  <pre><code> select * from a inner join b inner join c on a.idA = b.idB and b.lastName = c.lastName and a.name = c.name 
  </code></pre>
  
  <p>in HQL and JPA</p>