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