public abstract class

DataAccessUtils

extends Object
java.lang.Object
   ↳ org.springframework.dao.support.DataAccessUtils

Class Overview

Miscellaneous utility methods for DAO implementations. Useful with any data access technology.

Summary

Public Constructors
DataAccessUtils()
Public Methods
static int intResult(Collection results)
Return a unique int result from the given Collection.
static long longResult(Collection results)
Return a unique long result from the given Collection.
static <T> T objectResult(Collection<?> results, Class<T> requiredType)
Return a unique result object from the given Collection.
static <T> T requiredSingleResult(Collection<T> results)
Return a single result object from the given Collection.
static <T> T requiredUniqueResult(Collection<T> results)
Return a unique result object from the given Collection.
static <T> T singleResult(Collection<T> results)
Return a single result object from the given Collection.
static RuntimeException translateIfNecessary(RuntimeException rawException, PersistenceExceptionTranslator pet)
Return a translated exception if this is appropriate, otherwise return the input exception.
static <T> T uniqueResult(Collection<T> results)
Return a unique result object from the given Collection.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public DataAccessUtils ()

Public Methods

public static int intResult (Collection results)

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.

Parameters
results the result Collection (can be null)
Returns
  • the unique int result
Throws
IncorrectResultSizeDataAccessException if more than one result object has been found in the given Collection
EmptyResultDataAccessException if no result object at all has been found in the given Collection
TypeMismatchDataAccessException if the unique object in the collection is not convertable to an int

public static long longResult (Collection results)

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.

Parameters
results the result Collection (can be null)
Returns
  • the unique long result
Throws
IncorrectResultSizeDataAccessException if more than one result object has been found in the given Collection
EmptyResultDataAccessException if no result object at all has been found in the given Collection
TypeMismatchDataAccessException if the unique object in the collection is not convertable to a long

public static T objectResult (Collection<?> results, Class<T> requiredType)

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.

Parameters
results the result Collection (can be null)
Returns
  • the unique result object
Throws
IncorrectResultSizeDataAccessException if more than one result object has been found in the given Collection
EmptyResultDataAccessException if no result object at all has been found in the given Collection
TypeMismatchDataAccessException if the unique object does not match the specified required type

public static T requiredSingleResult (Collection<T> results)

Return a single result object from the given Collection.

Throws an exception if 0 or more than 1 element found.

Parameters
results the result Collection (can be null)
Returns
  • the single result object
Throws
IncorrectResultSizeDataAccessException if more than one element has been found in the given Collection
EmptyResultDataAccessException if no element at all has been found in the given Collection

public static T requiredUniqueResult (Collection<T> results)

Return a unique result object from the given Collection.

Throws an exception if 0 or more than 1 instance found.

Parameters
results the result Collection (can be null)
Returns
  • the unique result object
Throws
IncorrectResultSizeDataAccessException if more than one result object has been found in the given Collection
EmptyResultDataAccessException if no result object at all has been found in the given Collection

public static T singleResult (Collection<T> results)

Return a single result object from the given Collection.

Returns null if 0 result objects found; throws an exception if more than 1 element found.

Parameters
results the result Collection (can be null)
Returns
  • the single result object, or null if none
Throws
IncorrectResultSizeDataAccessException if more than one element has been found in the given Collection

public static RuntimeException translateIfNecessary (RuntimeException rawException, PersistenceExceptionTranslator pet)

Return a translated exception if this is appropriate, otherwise return the input exception.

Parameters
rawException exception we may wish to translate
pet PersistenceExceptionTranslator to use to perform the translation
Returns
  • a translated exception if translation is possible, or the raw exception if it is not

public static T uniqueResult (Collection<T> results)

Return a unique result object from the given Collection.

Returns null if 0 result objects found; throws an exception if more than 1 instance found.

Parameters
results the result Collection (can be null)
Returns
  • the unique result object, or null if none
Throws
IncorrectResultSizeDataAccessException if more than one result object has been found in the given Collection