-->
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.  [ 2 posts ] 
Author Message
 Post subject: Way to optimize this?
PostPosted: Tue Jan 26, 2010 5:29 pm 
Newbie

Joined: Wed Oct 14, 2009 3:22 pm
Posts: 16
Hi,

I'm using Hibernate 3.3.2.GA. I am given a list of object ids, for which I wish to look up an object. Right now, I'm iterating through the ids and looking up the objects one at a time, but I suspect there's a more efficient way to do this (i.e. loading all the objects at once by passing all the ids). Here's what I have so far ...

Code:
   public void markInvoicesAsPaid(List<Integer> paymentIds)
         throws RemoteException {
      Session session = null;
      try {
         session = HibernateFactory.getSessionFactory().openSession();
         Transaction tx = session.beginTransaction();
         for (Integer paymentId : paymentIds) {
            Criteria criteria = session
               .createCriteria(FinancialAidPayment.class);
            criteria.add(Restrictions.eq("id", paymentId));
            FinancialAidPayment payment = (FinancialAidPayment) criteria.uniqueResult();
            if (payment.getPaidDate() == null) {
               Calendar today = Calendar.getInstance();
               payment.setPaidDate(today.getTime());
               session.saveOrUpdate(payment);
            }
         } // for
         tx.commit();
      } catch (Throwable e) {
         final String errMsg = "Error in getApplicant";
         log.error(errMsg, e);
         throw new RemoteException(errMsg, e);
      } finally {
         try {
            session.close();
         } catch (Exception e) {
            log.warn("Unable to close session:" + e.getCause().toString());
         }
      }
   }


How can I optimize this to reduce the number of calls to the database?

Thanks, - Dave


Top
 Profile  
 
 Post subject: Re: Way to optimize this?
PostPosted: Wed Jan 27, 2010 9:16 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
I would have done in this way.
Code:
String queryStr = " FROM Payment WHERE id in (:ids)";
session = HibernateFactory.getSessionFactory().openSession();
Query  query = sesssion.createQuery(queryStr);
query.setParameterList("ids", paymentIds);

List<Payment> p = query.list();


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

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.