title: JPA fetch eager with condition
body: <p>Technologies: JPA, eclipseLink, postgreSQL.</p>
  
  <p>I'm looking for a nice way to solve a problem I have:</p>
  
  <p>My datamodel contains an Employee-entity, that has a OneToMany relationship with the entity Task, that has a startDate and an endDate (and some more attributes).
  When I am loading an employee, I always need the tasks as well, so I have set the fetchtype to eager.
  The problem is, the longer the application is used, an employee has more and more tasks referenced, but most of them are (far) in the past.</p>
  
  <p>So to prevent the application performance from suffering, I am looking for a JPA-annotation-based way to define conditions for the fetchtype, in this case, I only want to load the tasks for an employee, that have an endDate after last midnight.</p>
  
  <p>I have found a solution with the annotation @PostLoad (see code below), but I was wondering if there is a possibility to do the same thing with an annotation on the field.</p>
  
  <p>What is, in your opinion, the nicest way to do this?</p>
  
  <p>Thanks for the help!</p>
  
  <pre><code>@Entity
  @Table(name = "tbl_employee")
  public class Employee implements Serializable {
  
      //... (attributes)
  
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "employee")
      private List&lt;Task&gt; tasks;
  
      @PostLoad
      public void loadTasks() {
          tasks = new TaskDao().getTasksByEmployeeByToday(this);
      }
  
      //... (getters, setters)
  
  }
  
  
  
  @Entity
  @Table(name = "tbl_task")
  public class Task implements Serializable {
  
      //... (attributes)
  
      @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      @JoinColumn(name = "fi_employee", referencedColumnName = "id_employee")
      private Employee employee;
  
      @Column(name = "start_date", nullable = true)
      private Timestamp startDate;
  
      @Column(name = "end_date", nullable = true)
      private Timestamp endDate;
  
      //... (getters, setters)
  }
  
  
  public class TaskDao extends GenericDaoCRUD&lt;Task&gt; {
  
      public List&lt;Task&gt; getTasksByEmployeeByDate(Employee employee, Timestamp date) {
          Timestamp lastMidnight = Utils.getLastMidnight(date);
  
          String statement = "SELECT t FROM Task t WHERE t.employee = :employee and t.endDate &gt; :enddate";
  
          TypedQuery&lt;Task&gt; query = getEntityManager().createQuery(statement, Task.class);
          query.setParameter("employee", employee);
          query.setParameter("enddate", lastMidnight);
  
          try {
              return query.getResultList();
          } catch (NoResultException e) {
              LOG.info("No tasks found for employee=" + employee + "; and date&gt;"
                      + lastMidnight);
          }
          return null;
      }
  
      public List&lt;Task&gt; getTasksByEmployeeByToday(Employee employee) {
          return getTasksByEmployeeByDate(employee, new Timestamp(System.currentTimeMillis()));
      }
  </code></pre>