title: JPA one to many relationship query
body: <p>Implemented one to many relationship and it is working fine.</p>
  
  <p>My issue is when i run the below query, if the table has 100 employee rows, and each employee has 2 departments. The database query is called 101 times, because for each employee it is calling department query, it is taking very long to complete calling all hundred rows, can any one suggest any alternative solution?</p>
  
  <p>Please see the details below</p>
  
  <p>Queries it is calling:</p>
  
  <pre><code>    First query is :    SELECT * FROM Employee e
  
      Next 100 queries : SELECT * FROM DEPARTMENT d WHERE d.EmployeeId=?
  </code></pre>
  
  <p>JPA Database call :         </p>
  
  <pre><code>    javax.persistence.Query query = em.createNamedQuery("SELECT * FROM Employee e", Employee.class);
  
      return query.getResultList();
  
  
  
  
      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.Id;
      import javax.persistence.NamedNativeQueries;
      import javax.persistence.NamedNativeQuery;
      import javax.persistence.OneToMany;
      import javax.persistence.Table;
  
      @Entity
      @Table(name = "EMPLOYEE")
      public class Employee implements Serializable
      {
          @Id
          @Column(name = "EmployeeId")
          String employeeId;
  
          @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
          private List&lt;Department&gt; departments;
  
          public List&lt;Department&gt; getDepartments() {
              return departments;
          }
  
          public void setDepartments(List&lt;Department&gt; departments) {
              this.departments = departments;
          }
  
          public String getEmployeeId() {
              return employeeId;
          }
  
          public void setEmployeeId(String employeeId) {
              this.employeeId = employeeId;
          }
      }
  
      @Entity
      @Table(name = "DEPARTMENT")
      public class Department implements Serializable
      {
          private static final long serialVersionUID = 1L;
  
          @Id
          @Column(name = "DepartmentID")
          String departmentId;
  
          @ManyToOne(fetch = FetchType.EAGER)
          @JoinColumn(name = "EmployeeId", insertable = false, updatable = false)
          private Employee employee;
      }
  </code></pre>
  
  <p>The output xml:                        </p>
  
  <pre><code>        &lt;Employees&gt;
              &lt;Employee&gt;
                  &lt;name&gt;Rob&lt;/name&gt;
                  &lt;Departments&gt;
                      &lt;Departmnet&gt;&lt;id&gt;1&lt;/id&gt;&lt;/Departmnet&gt;
                      &lt;Departmnet&gt;&lt;id&gt;2&lt;/id&gt;&lt;/Departmnet&gt;
                  &lt;/Departments&gt;  
              &lt;/Employee&gt;
              &lt;Employee&gt;
                  &lt;name&gt;Sam&lt;/name&gt;
                  &lt;Departments&gt;
                      &lt;Departmnet&gt;&lt;id&gt;1&lt;/id&gt;&lt;/Departmnet&gt;
                      &lt;Departmnet&gt;&lt;id&gt;2&lt;/id&gt;&lt;/Departmnet&gt;
                  &lt;/Departments&gt;  
              &lt;/Employee&gt;
          &lt;/Employees&gt;
  </code></pre>