title: How to handle large dataset with JPA (or at least with Hibernate)?
body: <p>I need to make my web-app work with really huge datasets. At the moment I get either OutOfMemoryException or output which is being generated 1-2 minutes.</p>
  
  <p>Let's put it simple and suppose that we have 2 tables in DB: <code>Worker</code> and <code>WorkLog</code> with about 1000 rows in the first one and 10 000 000 rows in the second one. Latter table has several fields including 'workerId' and 'hoursWorked' fields among others. What we need is:</p>
  
  <ol>
  <li><p>count total hours worked by each user; </p></li>
  <li><p>list of work periods for each user.</p></li>
  </ol>
  
  <p>The most straightforward approach (IMO) for each task in plain SQL is:</p>
  
  <p>1) </p>
  
  <pre><code>select Worker.name, sum(hoursWorked) from Worker, WorkLog 
     where Worker.id = WorkLog.workerId 
     group by Worker.name;
  
  //results of this query should be transformed to Multimap&lt;Worker, Long&gt;
  </code></pre>
  
  <p>2) </p>
  
  <pre><code>select Worker.name, WorkLog.start, WorkLog.hoursWorked from Worker, WorkLog
     where Worker.id = WorkLog.workerId;
  
  //results of this query should be transformed to Multimap&lt;Worker, Period&gt;
  //if it was JDBC then it would be vitally 
  //to set resultSet.setFetchSize (someSmallNumber), ~100
  </code></pre>
  
  <p>So, I have two questions: </p>
  
  <ol>
  <li>how to implement each of my approaches with JPA (or at least with Hibernate); </li>
  <li>how would you handle this problem (with JPA or Hibernate of course)?</li>
  </ol>