title: How to join multiple tables in hibernate
body: <p>I am developing a web application using hibernate.</p>
  
  <p>The same application i have created in jsp servlet but now i am using <code>spring</code> and <code>hibernate</code> but unable to do same thing which i did using <code>jsp/servlet</code></p>
  
  <p>jsp code</p>
  
  <pre><code>String sql = "select city from addres_table where id=2";
  Statement st = con.createStatement();
  ResultSet rs = st.executeQuery(sql);
  while(rs.next()){
  String city = rs.getString("city");
  String sql1 = "select name from user_table where city='"+city+"'";
  Statement st1 = con.createStatement();
  ResultSet rs1 = st1.executeQuery(sql1);
  while(rs1.next()){
  String username = rs.getString("name");
  }
  }
  </code></pre>
  
  <p>Basically i am fetching <code>city</code> from <code>addres_table</code> now i am fetching <code>name</code> of <code>users</code> who are living in that <code>city</code> from <code>User_table</code>.</p>
  
  <p>Same thing i want to do in hibernate but i am unable to do. </p>
  
  <p>I have fetched <code>city</code> from <code>addres_table</code> but now how can i fetch <code>name</code> from <code>User_table</code> using that city. </p>
  
  <p>Here is my hibernate code</p>
  
  <pre><code>@SuppressWarnings({ "unchecked", "rawtypes" })
  public List&lt;Address&gt; getProfessionById(long id) throws Exception {
  session = sessionFactory.openSession();
  Criteria cr = session.createCriteria(Address.class);
  cr.add(Restrictions.eq("uid", id));
  List results = cr.list();
  tx = session.getTransaction();
  session.beginTransaction();
  tx.commit();
  return results;
  }
  </code></pre>
  
  <p>Please help me i want to fetch name from User_table using this city</p>
  
  <p>Address class</p>
  
  <pre><code>@Entity
  @Table(name="user_profession")
  @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  public class Address implements Serializable {
  
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue
  @Column(name="id")
  private long id;
  
  @Column(name="city")
  private String city;
  
  public long getId() {
      return id;
  }
  
  
  public void setId(long id) {
      this.id = id;
  }
  
  public String getCity() {
      return city;
  }
  
      public void setCity(String city) {
      this.city = city;
  }
  }
  </code></pre>
  
  <p>User class</p>
  
  <pre><code>@Entity
  @Table(name="user_table")
  @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  public class User implements Serializable {
  
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue
  @Column(name="id")
  private long id;
  
  @Column(name="full_name")
  private String fullName;
  
  public long getId() {
      return id;
  }
  public void setId(long id) {
      this.id = id;
  }
  
  public String getFullName() {
      return fullName;
  }
  public void setFullName(String fullName) {
      this.fullName = fullName;
  }
  }
  </code></pre>