OPENJPA-208 Added NoResultException and NonUniqueResultException to kernel to allow the identification of the case where a unique result was selected by either none or too many were found

git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@526834 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Marc Prud'hommeaux 2007-04-09 17:06:23 +00:00
parent 82c71f6dce
commit 0806fd5b44
6 changed files with 108 additions and 20 deletions

View File

@ -51,6 +51,8 @@ import org.apache.openjpa.meta.JavaTypes;
import org.apache.openjpa.meta.MetaDataRepository; import org.apache.openjpa.meta.MetaDataRepository;
import org.apache.openjpa.util.GeneralException; import org.apache.openjpa.util.GeneralException;
import org.apache.openjpa.util.InvalidStateException; import org.apache.openjpa.util.InvalidStateException;
import org.apache.openjpa.util.NonUniqueResultException;
import org.apache.openjpa.util.NoResultException;
import org.apache.openjpa.util.OpenJPAException; import org.apache.openjpa.util.OpenJPAException;
import org.apache.openjpa.util.UnsupportedException; import org.apache.openjpa.util.UnsupportedException;
import org.apache.openjpa.util.UserException; import org.apache.openjpa.util.UserException;
@ -1279,10 +1281,10 @@ public class QueryImpl
if (next) { if (next) {
single = rop.getResultObject(); single = rop.getResultObject();
if (range.end != range.start + 1 && rop.next()) if (range.end != range.start + 1 && rop.next())
throw new InvalidStateException(_loc.get("not-unique", throw new NonUniqueResultException(_loc.get("not-unique",
_class, _query)); _class, _query));
} else if (_unique == Boolean.TRUE) } else if (_unique == Boolean.TRUE)
throw new InvalidStateException(_loc.get("no-result", throw new NoResultException(_loc.get("no-result",
_class, _query)); _class, _query));
// if unique set to false, use collection // if unique set to false, use collection

View File

@ -0,0 +1,43 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.openjpa.util;
import org.apache.openjpa.lib.util.Localizer.Message;
/**
* Exception type thrown when a query was configured to return
* a single result, and no result was found.
*
* @since 0.9.7
* @author Marc Prud'hommeaux
*/
public class NoResultException
extends InvalidStateException {
public NoResultException(Message msg) {
super(msg);
}
public NoResultException(Message msg, Object failed) {
super(msg);
setFailedObject(failed);
}
public int getSubtype() {
return NO_RESULT;
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.openjpa.util;
import org.apache.openjpa.lib.util.Localizer.Message;
/**
* Exception type thrown when a query was configured to return
* a single result, and multiple results were found.
*
* @since 0.9.7
* @author Marc Prud'hommeaux
*/
public class NonUniqueResultException
extends InvalidStateException {
public NonUniqueResultException(Message msg) {
super(msg);
}
public NonUniqueResultException(Message msg, Object failed) {
super(msg);
setFailedObject(failed);
}
public int getSubtype() {
return NON_UNIQUE_RESULT;
}
}

View File

@ -30,6 +30,8 @@ public class UserException
public static final int INVALID_STATE = 2; public static final int INVALID_STATE = 2;
public static final int NO_TRANSACTION = 3; public static final int NO_TRANSACTION = 3;
public static final int CALLBACK = 4; public static final int CALLBACK = 4;
public static final int NO_RESULT = 5;
public static final int NON_UNIQUE_RESULT = 6;
public UserException() { public UserException() {
} }

View File

@ -173,6 +173,16 @@ public class PersistenceExceptions
(ke.getMessage(), getNestedThrowables(ke), (ke.getMessage(), getNestedThrowables(ke),
getFailedObject(ke), ke.isFatal()); getFailedObject(ke), ke.isFatal());
break; break;
case UserException.NO_RESULT:
e = new org.apache.openjpa.persistence.NoResultException
(ke.getMessage(), getNestedThrowables(ke),
getFailedObject(ke), ke.isFatal());
break;
case UserException.NON_UNIQUE_RESULT:
e = new org.apache.openjpa.persistence.NonUniqueResultException
(ke.getMessage(), getNestedThrowables(ke),
getFailedObject(ke), ke.isFatal());
break;
case UserException.INVALID_STATE: case UserException.INVALID_STATE:
e = new org.apache.openjpa.persistence.InvalidStateException e = new org.apache.openjpa.persistence.InvalidStateException
(ke.getMessage(), getNestedThrowables(ke), (ke.getMessage(), getNestedThrowables(ke),

View File

@ -281,26 +281,14 @@ public class QueryImpl
*/ */
public Object getSingleResult() { public Object getSingleResult() {
_em.assertNotCloseInvoked(); _em.assertNotCloseInvoked();
Object ob = execute(); // temporarily set query to unique so that a single result is validated
if (!(ob instanceof List)) // and returned; unset again in case the user executes query again
return ob; // via getResultList
_query.setUnique(true);
List res = (List) ob;
try { try {
// don't use size() b/c can be inefficient under some LRS settings return execute();
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 {
OpenJPAPersistence.close(res); _query.setUnique(false);
} }
} }