OPENJPA-208 Throw NoResultException and NonUniqueResultException when expected

git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@526322 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Marc Prud'hommeaux 2007-04-06 23:52:03 +00:00
parent 840644fcc5
commit af2550bb05
1 changed files with 18 additions and 6 deletions

View File

@ -281,14 +281,26 @@ public class QueryImpl
*/ */
public Object getSingleResult() { public Object getSingleResult() {
_em.assertNotCloseInvoked(); _em.assertNotCloseInvoked();
// temporarily set query to unique so that a single result is validated Object ob = execute();
// and returned; unset again in case the user executes query again if (!(ob instanceof List))
// via getResultList return ob;
_query.setUnique(true);
List res = (List) ob;
try { try {
return execute(); // don't use size() b/c can be inefficient under some LRS settings
Iterator itr = res.iterator();
if (!itr.hasNext())
throw new NoResultException(_loc.get("no-results",
_query.getQueryString()).getMessage(), null, null, false);
Object ret = itr.next();
if (itr.hasNext())
throw new NonUniqueResultException(_loc.get("mult-results",
_query.getQueryString()).getMessage(), null, null, false);
return ret;
} finally { } finally {
_query.setUnique(false); OpenJPAPersistence.close(res);
} }
} }