loading huge amount of data like more than one million of row can make a big problem, OutOfMemory Java error for example, so what can we do on such situation? with my little experience i can suggest two solutions, the first is the use of Criteria class and the second one is the ScrollableResults class.
- Criteria class :
with Creteria we can make pagination solution, this means that we split the result into differents pages to make it easy to load and process, see the code below :Criteria criteria = session.createCriteria(HistoryRecord.class); Criteria criteria = session.createCriteria(HistoryRecord.class).addOrder(Order.asc("CreatedDate") ); criteria.setMaxResults(10); criteria.setFirstResult(20); criteria.list();
these code let us take only 10 objects from index 20, even we have one million persisted objects into data base,we can avoid OutofMemory error. these code is not completed because it is static solution that take only object between 20 and 30 index. To be able to progressively load pages, setFirstResult() parametre should be dynamic because it represent the first index of the page, also this code should be called on demand. - ScrollableResults class
if you need to read each object separatly from a huge amout of data base objects instead of a complet page, you can use ScrollableResults class that make a iterator allows moving around within the results. See the below code :
Query query = session.createQuery(HistoryRecord.class); ScrollableResults results = query.scroll(); while (results.next()) { HistoryRecord obj = (HistoryRecord) results.get() // make some instructions ... } results.close();