Question: I have a table with 62,000,000 rows aprox, a need select data from these a export to .txt or .csv

My query limit the result to 60,000 rows aprox.

When I run my the query in my developer machine, I eat all memory and get a `java.lang.OutOfMemoryError`

In this moment I use Hibernate for DAO, but I can change to pure JDBC solution when you recommend

My pseoudo-code is

```
List<Map> list = myDao.getMyData(Params param); //program crash here

initFile();
for(Map map : list){
    util.append(map); //this transform row to file
}
closeFile();

```

Suggesting me to write my file?

Note: I use `.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);` to get `Map` instead of any `Entity`

Answer: 
You could use hibernate's `ScrollableResults`. See documentation here: <http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch11.html#objectstate-querying-executing-scrolling>

This uses server-side cursors, if your database engine / database driver supports this. Be sure for this to work you set the following properties:

```
query.setReadOnly(true);
query.setCacheable(false);
ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);

while (results.next()) {
    SomeEntity entity = results.get()[0];
}
results.close();

```
lock the table and then perform subset selection and exports, appending to the results file. ensure you unconditionally unlock when done.
Not nice, but the task will perform to completion even on limited resource servers or clients.