There has to be cleaner way of writing the following using Spring's HibernateTemplate....
List mailLogs = getHibernateTemplate().find("from MailLogValue m where m.mailLogId = ?", id);
if (mailLogs.size() == 1) {
return (MailLogValue) mailLogs.get(0);
} else {
throw new InvalidMessageException("Invalid message id " + id);
}
I currently have about 10 of these constructs, changing only by their arguments, or class types. Surely theres a cleaner way around this?
try {
return (MailLogValue) DetachedCriteria.forClass(MailLogValue.class)
.add(Expression.eq("guid", guid)).getExecutableCriteria(getSession()).uniqueResult();
} catch (Exception e) {
throw new InvalidMessageException("Invalid message guid " + guid);
}
This would seem to be a viable compromise apart from the global Exception catch/rethrow...
How about:
import org.springframework.dao.support.DataAccessUtils;
...
...
return (MailLogValue)DataAccessUtils.requiredUniqueResult(
getHibernateTemplate().find("from MailLogValue m where m.mailLogId
= ?", id));
?
On top of that there are other helpful methods in DataAccessUtils.
HTH
Fokko