title: Large OpenJPA query.getResutList() wrapper to avoid List<MyObject> and then iterate
body: <p>Version <code>openjpa-2.4.1=2.4.1.SNAPSHOT</code></p>
  
  <p>I have a complex sql run as a native query in OpenJPA. Some of the value filtering is done at the application side when looping through the arraylist.</p>
  
  <p>This means <code>List&lt;MyObject&gt;</code> is held in a memory before any rows are written to a servlet output stream. Not to mention MySQL jdbc resultset is fully read to RAM because of how mysql jdbc driver works. So rows are held in RAM two times some period of time.</p>
  
  <p>Is it possible to have a customized progressive row handler when calling a <code>query.getResultList()</code> function? I could write only meaninful jdbc rows to json response and skip those not interested in.</p>
  
  <p>Or is there a way to use a raw jdbc resultset and cast row to MyObject jpa object instance in an application side? It does need to be managed instance but would like to reuse an existing serialization from sql to object.</p>
  
  <p><strong>Current code</strong></p>
  
  <pre><code>Query query = em.createNativeQuery(sql, MyObject.class);
  query.setParameter(1, "serverX");
  List&lt;MyObject&gt; list=query.getResultList();
  
  JsonGenerator jsonG = MyApplication.createJsonGenerator(providers, os);
  jsonG.writeStartObject();
  jsonG.writeFieldName("items");
  jsonG.writeStartArray();
  for(MyObject obj : list) {
    if (obj.getType()==1) MyObject.writeJSONv1(jsong, obj);
    else if (obj.getType()==2) MyObject.writeJSONv2(jsong, obj);
    else if (obj.isValid() &amp;&amp; obj.getType()==3) MyObject.writeJSONv3(jsong, obj);
    else if (obj.getName().startsWith("abc")) MyObject.writeJSONvX(jsong, obj);
    else Logger.log("Skipped " + obj.getId());
  }
  jsonG.writeEndArray();
  jsonG.writeEndObject();
  jsonG.flush();
  jsonG.close();
  </code></pre>
  
  <p><strong>Pseudo code, progressive iterator</strong></p>
  
  <pre><code>Query query = em.createNativeQuery(sql, MyObject.class);
  query.setParameter(1, "serverX");
  Iterator iter=query.getCustomProgressiveRowIterator();
  
  JsonGenerator jsonG = MyApplication.createJsonGenerator(providers, os);
  jsonG.writeStartObject();
  jsonG.writeFieldName("items");
  jsonG.writeStartArray();
  for(iter.hasNext()) {
    MyObject obj = (MyObject)iter.next();
    if (obj.getType()==1) MyObject.writeJSONv1(jsong, obj);
    else if (obj.getType()==2) MyObject.writeJSONv2(jsong, obj);
    else if (obj.isValid() &amp;&amp; obj.getType()==3) MyObject.writeJSONv3(jsong, obj);
    else if (obj.getName().startsWith("abc")) MyObject.writeJSONvX(jsong, obj);
    else Logger.log("Skipped " + obj.getId());
  }
  jsonG.writeEndArray();
  jsonG.writeEndObject();
  jsonG.flush();
  jsonG.close();
  </code></pre>
  
  <p><strong>Pseudo code, cast jdbc row to object</strong></p>
  
  <pre><code>  PreparedStatement stmt = con.prepareStatement("Select * my complex query From MyObjects");
    ResultSet rs = stmt.executeQuery();
    while(rs.next()) {
      // check few resultset fields, if all good then get MyObject
      MyObject obj = OpenJPA.castResultSetToObject(rs);
      MyObject.writeJSON(jsong, obj);
    }
    rs.close();
    stmt.close();
  </code></pre>
  
  <p>Edit:
  @Gimby had a comment about getResultStream(), I link it here for later reference if OpenJPA has it one day. Also link to OpenJPA 3.x ticket.
  <br/>
  <a href="https://www.thoughts-on-java.org/jpa-2-2s-new-stream-method-and-how-you-should-not-use-it/" rel="nofollow noreferrer">https://www.thoughts-on-java.org/jpa-2-2s-new-stream-method-and-how-you-should-not-use-it/</a> 
  <br/>
  <a href="https://issues.apache.org/jira/browse/OPENJPA-2711" rel="nofollow noreferrer">https://issues.apache.org/jira/browse/OPENJPA-2711</a></p>