title: Hibernate joining two table
body: <p>It is only recently that I am on top of the hibernate framwork and I can not get a join between two tables as a result. Could you help me understand where I'm wrong?</p>
  
  <p>My Oracle Query is:</p>
  
  <pre><code>select * from ld_folder_scan ls, ld_folder_scan_content lss where ls.folder_id = lss.folder_id and ls.folder_id = 1;
  </code></pre>
  
  <p>I have two entity class ld_folder_scan and ld_folder_scan_content.I need to join both the tables and fetch all records which matching the given condition.</p>
  
  <pre><code>ld_folder_scan
  +-----------+----------------------------+-------------+------------+
  | folder_id | status_folder_scan_type_id | folder_name | service_id |
  +-----------+----------------------------+-------------+------------+
  |  1        | Sent                       | F001        |      1     |
  |  2        | Receive                    | F002        |      1     |
  +-----------+----------------------------+-------------+------------+
  
  ld_folder_scan_content
  +-----------+-------------+
  | folder_id | document_id | 
  +-----------+-------------+
  |  1        |      1      |
  |  1        |      2      |
  |  1        |      3      |
  |  1        |      4      |
  |  2        |      5      |
  |  2        |      6      |
  |  2        |      7      |
  |  2        |      8      |
  |  2        |      9      |
  +-----------+-------------+
  </code></pre>
  
  <p>What I would like to get from the query result:</p>
  
  <pre><code>+-----------+----------------------------+-------------+------------+-------------+
  | folder_id | status_folder_scan_type_id | folder_name | service_id | document_id | 
  +-----------+----------------------------+-------------+------------+-------------+
  |  1        | Sent                       | F001        |      1     |      1      |
  |  1        | Sent                       | F001        |      1     |      2      |
  |  1        | Sent                       | F001        |      1     |      3      |
  |  1        | Sent                       | F001        |      1     |      4      |
  +-----------+----------------------------+-------------+------------+-------------+
  </code></pre>
  
  <p>The Java Code:</p>
  
  <pre><code>package org.hibernate;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "ld_folder_scan")
  public class ld_folder_scan {
  
      @Id
      @GeneratedValue
      private long folder_id;
      private short status_folder_scan_type_id;
      private String folder_name;
      private short service_id;
  
  
      @OneToMany( cascade = CascadeType.ALL, orphanRemoval = true )
      @JoinColumn(name = "folder_id")
      private List&lt;ld_scatole_scan_content&gt; ld_scatole_scan_content = new ArrayList&lt;&gt;();
  
      public long getFolder_id() {
          return folder_id;
      }
  
      public void setFolder_id(long folder_id) {
          this.folder_id = folder_id;
      }
  
      public short getStatus_folder_scan_type_id() {
          return status_folder_scan_type_id;
      }
  
      public void setStatus_folder_scan_type_id(short status_folder_scan_type_id) {
          this.status_folder_scan_type_id = status_folder_scan_type_id;
      }
  
      public String getFolder_name() {
          return folder_name;
      }
  
      public void setFolder_name(String folder_name) {
          this.folder_name = folder_name;
      }
  
      public short getService_id() {
          return service_id;
      }
  
      public void setService_id(short service_id) {
          this.service_id = service_id;
      }
  
      public void setLd_folder_scan_content(List&lt;ld_folder_scan_content&gt; ld_folder_scan_content) {
          this.ld_folder_scan_content = ld_folder_scan_content;
      }
  }
  
  ---------------------------------------------------
  
  package org.hibernate;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  
      @Entity
      @Table(name = "ld_folder_scan_content")
      public class ld_folder_scan_content {
  
      @Id
      @GeneratedValue
      private long folder_id;
      private long document_id;
  
      public long getFolder_id() {
          return folder_id;
      }
  
      public void setFolder_id(long folder_id) {
          this.folder_id = folder_id;
      }
  
      public long getDocument_id() {
          return lavorazione_id;
      }
  
      public void setDocument_id(long document_id) {
          this.document_id = document_id;
      }
  }
  
  
  ---------------------------------------------------------------------------
  
  package org.hibernate;
  
  import java.util.List;
  
  import org.hibernate.cfg.Configuration;
  
  public class OracleConnection {
  
      public static void main(String[] args) {
          SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
          Session session = sessionFactory.openSession();
          session.beginTransaction();
  
          String hql = "from ld_folder_scan where folder_id = 1";
          List&lt;ld_scatole_scan&gt; list = (List&lt;ld_folder_scan&gt;)session.createQuery(hql).getResultList();
          for(ld_folder_scan sca : list){
              for(int i=0;i&lt;sca.getLd_folder_scan_content().size();i++){
                  System.out.println("Folder Name::"+sca.getFolder_name() + " Document Id::" +sca.getLd_folder_scan_content().get(i).getDocument_id());
              }
          }
          session.getTransaction().commit();
      }
  }
  </code></pre>
  
  <p>What I get by debugging:</p>
  
  <pre><code>Folder Name::F001 Document Id::1
  Folder Name::F001 Document Id::1
  Folder Name::F001 Document Id::1
  Folder Name::F001 Document Id::1
  </code></pre>
  
  <p>What I would like to get:</p>
  
  <pre><code>Folder Name::F001 Document Id::1
  Folder Name::F001 Document Id::2
  Folder Name::F001 Document Id::3
  Folder Name::F001 Document Id::4
  </code></pre>
  
  <p>Where am I wrong?</p>
  
  <p>Thanks.</p>