openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mprud...@apache.org
Subject svn commit: r526322 - /incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Date Fri, 06 Apr 2007 23:52:04 GMT
Author: mprudhom
Date: Fri Apr  6 16:52:03 2007
New Revision: 526322

URL: http://svn.apache.org/viewvc?view=rev&rev=526322
Log:
OPENJPA-208 Throw NoResultException and NonUniqueResultException when expected

Modified:
    incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java

Modified: incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java?view=diff&rev=526322&r1=526321&r2=526322
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
(original)
+++ incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Fri Apr  6 16:52:03 2007
@@ -281,14 +281,26 @@
      */
     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);
         }
     }
 



Mime
View raw message