Mark Derricutt's Disturbing Thoughts

My Top Tags

                                       

My Jaiku

The BGGA myth - Functional Java | Google Groups

Wednesday, 20 August 2008 7:33 A GMT+12

Spock's Beard – On A Perfect Day (live)

Wednesday, 20 August 2008 5:57 A GMT+12

Ola Bini: JtestR 0.3.1 Released

Tuesday, 19 August 2008 10:00 P GMT+12

Enslaved – Violet Dawning

Tuesday, 19 August 2008 6:14 A GMT+12

Distributed Messaging with Jetlang and Terracotta

Monday, 18 August 2008 10:17 P GMT+12

The Music of 2008 - week 33

Monday, 18 August 2008 8:57 A GMT+12

There Can Be Only One

Monday, 18 August 2008 8:10 A GMT+12

Silent Force – Point Of No Return

Monday, 18 August 2008 6:30 A GMT+12

In Relation To...  Hibernate Core 3.3.0 goes GA

Saturday, 16 August 2008 7:03 P GMT+12

Search Box

 

Unique Items with Spring's HibernateTemplate - there must be a cleaner way?

posted Monday, 19 December 2005

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...

tags:      

links: digg this    del.icio.us    technorati    reddit




1. F. Degenaar left...
Monday, 19 December 2005 11:58 pm

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


2. Ian Joyce left...
Tuesday, 20 December 2005 3:59 am :: http://ianjoyce.org/

Why not use get?