mirror of https://github.com/apache/openjpa.git
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:
parent
840644fcc5
commit
af2550bb05
|
@ -281,14 +281,26 @@ public class QueryImpl
|
|||
*/
|
||||
public Object getSingleResult() {
|
||||
_em.assertNotCloseInvoked();
|
||||
// temporarily set query to unique so that a single result is validated
|
||||
// and returned; unset again in case the user executes query again
|
||||
// via getResultList
|
||||
_query.setUnique(true);
|
||||
Object ob = execute();
|
||||
if (!(ob instanceof List))
|
||||
return ob;
|
||||
|
||||
List res = (List) ob;
|
||||
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 {
|
||||
_query.setUnique(false);
|
||||
OpenJPAPersistence.close(res);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue