Miscellaneous utility methods for DAO implementations.
Useful with any data access technology.
| Method from org.springframework.dao.support.DataAccessUtils Detail: |
public static int intResult(Collection results) throws TypeMismatchDataAccessException, IncorrectResultSizeDataAccessException {
return ((Number) objectResult(results, Number.class)).intValue();
}
Return a unique int result from the given Collection.
Throws an exception if 0 or more than 1 result objects found,
of if the unique result object is not convertable to an int. |
public static long longResult(Collection results) throws TypeMismatchDataAccessException, IncorrectResultSizeDataAccessException {
return ((Number) objectResult(results, Number.class)).longValue();
}
Return a unique long result from the given Collection.
Throws an exception if 0 or more than 1 result objects found,
of if the unique result object is not convertable to a long. |
public static Object objectResult(Collection results,
Class requiredType) throws TypeMismatchDataAccessException, IncorrectResultSizeDataAccessException {
Object result = requiredUniqueResult(results);
if (requiredType != null && !requiredType.isInstance(result)) {
if (String.class.equals(requiredType)) {
result = result.toString();
}
else if (Number.class.isAssignableFrom(requiredType) && Number.class.isInstance(result)) {
try {
result = NumberUtils.convertNumberToTargetClass(((Number) result), requiredType);
}
catch (IllegalArgumentException ex) {
throw new TypeMismatchDataAccessException(ex.getMessage());
}
}
else {
throw new TypeMismatchDataAccessException(
"Result object is of type [" + result.getClass().getName() +
"] and could not be converted to required type [" + requiredType.getName() + "]");
}
}
return result;
}
Return a unique result object from the given Collection.
Throws an exception if 0 or more than 1 result objects found,
of if the unique result object is not convertable to the
specified required type. |
public static Object requiredSingleResult(Collection results) throws IncorrectResultSizeDataAccessException {
int size = (results != null ? results.size() : 0);
if (size == 0) {
throw new EmptyResultDataAccessException(1);
}
if (results.size() > 1) {
throw new IncorrectResultSizeDataAccessException(1, size);
}
return results.iterator().next();
}
|
public static Object requiredUniqueResult(Collection results) throws IncorrectResultSizeDataAccessException {
int size = (results != null ? results.size() : 0);
if (size == 0) {
throw new EmptyResultDataAccessException(1);
}
if (!CollectionUtils.hasUniqueObject(results)) {
throw new IncorrectResultSizeDataAccessException(1, size);
}
return results.iterator().next();
}
|
public static Object singleResult(Collection results) throws IncorrectResultSizeDataAccessException {
int size = (results != null ? results.size() : 0);
if (size == 0) {
return null;
}
if (results.size() > 1) {
throw new IncorrectResultSizeDataAccessException(1, size);
}
return results.iterator().next();
}
|
public static RuntimeException translateIfNecessary(RuntimeException rawException,
PersistenceExceptionTranslator pet) {
Assert.notNull(pet, "PersistenceExceptionTranslator must not be null");
DataAccessException dex = pet.translateExceptionIfPossible(rawException);
return (dex != null ? dex : rawException);
}
Return a translated exception if this is appropriate,
otherwise return the input exception. |
public static Object uniqueResult(Collection results) throws IncorrectResultSizeDataAccessException {
int size = (results != null ? results.size() : 0);
if (size == 0) {
return null;
}
if (!CollectionUtils.hasUniqueObject(results)) {
throw new IncorrectResultSizeDataAccessException(1, size);
}
return results.iterator().next();
}
|