-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How to use Batch Process SELECT Statement in Hibernate for N
PostPosted: Sun Jan 19, 2014 2:24 am 
Newbie

Joined: Sun Jan 19, 2014 2:19 am
Posts: 1
Friends

When the query returns more than 1500 records , I want to chunk up the requests into multiple requests of 1500 each. How can we achieve this using Hibernate ScrollableResults or similar for native SQL query used in createSQLQuery ?

I have tried using simple technique but need more input on how we can use Hibernate ScrollableResults for Native SQL batch select records

Code:
   public List<EmployeeDetails> processRecordsInBatches() throws Exception {
         log.info("Inside getPendingStatus: ");
         Session s = getSession();
         Transaction tx = null;
          final int BATCH_SIZE = 1500;
         List<EmployeeDetails> result = null;
         ScrollableResults allEntities = null;

         try {
            tx = beginTransaction(s);
            

         if ( ++BATCH_SIZE % 1500 == 0 ){
            Query query = s.createSQLQuery("select c.employee_id  AS EmployeeId, 99 AS assignedProjectId, c.employee_name AS employeeName, " +
                  " from employee.employee_details c" ) ;

            query.setInteger("employeeId", employeeId );
            query.setString("assignedProjectId", assignedProjectId);
            query.setString("employee_name", employee_name);
            log.info(query);

            query.setResultTransformer(new ResultTransformer() {
               @Override
               public Object transformTuple(Object[] tuple, String[] aliases) {
                  EmployeeDetails details = new EmployeeDetails();
                  details.setEmployeeId((Integer)tuple[0]);
                  details.assignedProjectId((Integer)tuple[1]);
                  details.setEmployeeName((Integer)tuple[2]);
                  return details;
               }

               @Override
               public List transformList(List list) {
                  return list;
               }
            });

            result =  query.list();
            s.flush();
            s.clear();
            }
            commitTransaction(tx);
            log.info(result);


         } catch (Exception ex) {
            log.error(ex);
         } finally {
            closeSession(s);
         }
         return result;
      }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.