DB2 optimization for Query.getSingleResult()

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@741398 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2009-02-06 04:31:17 +00:00
parent b4a09c4dab
commit 72ac31544f
2 changed files with 15 additions and 5 deletions

View File

@ -22,6 +22,8 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.persistence.NoResultException;
import org.apache.openjpa.persistence.query.common.apps.RuntimeTest1;
import org.apache.openjpa.persistence.query.common.apps.RuntimeTest2;
@ -256,10 +258,12 @@ public class TestQueryResults extends BaseQueryTest {
query =
"SELECT DISTINCT r FROM RuntimeTest1 r WHERE r.stringField = \'xxxx\'";
OpenJPAQuery q = em.createQuery(query);
List l = q.getResultList();
assertNotNull(
"expecting l to be null since there is no RuntimeTest1 instance with stringfield=xxxx",
l);
try {
Object l = q.getSingleResult();
fail("Expected NoResultException since there is no RuntimeTest1 instance with stringfield=xxxx");
} catch (NoResultException e) {
// good
}
q.closeAll();
endTx(em);

View File

@ -288,6 +288,7 @@ public class QueryImpl implements OpenJPAQuerySPI, Serializable {
*/
public Object getSingleResult() {
_em.assertNotCloseInvoked();
setHint("openjpa.hint.OptimizeResultCount", 1); // for DB2 optimization
List result = getResultList();
if (result == null || result.isEmpty())
throw new NoResultException(_loc.get("no-result", getQueryString())
@ -295,7 +296,12 @@ public class QueryImpl implements OpenJPAQuerySPI, Serializable {
if (result.size() > 1)
throw new NonUniqueResultException(_loc.get("non-unique-result",
getQueryString(), result.size()).getMessage());
try {
return result.get(0);
} catch (Exception e) {
throw new NoResultException(_loc.get("no-result", getQueryString())
.getMessage());
}
}
public int executeUpdate() {