Top Description Constructors Methods
org.springframework.dao.support

public abstract Class DataAccessUtils

extends Object
Class Inheritance
Imports
java.util.Collection, .Iterator, .List, .Optional, java.util.stream.Stream, org.springframework.dao.DataAccessException, .EmptyResultDataAccessException, .IncorrectResultSizeDataAccessException, .TypeMismatchDataAccessException, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, .NumberUtils

Miscellaneous utility methods for DAO implementations.

Useful with any data access technology.

Author
Juergen Hoeller
Since
1.0.2

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static int

Returns:

the unique int result
intResult
(Collection<?>
the result Collection (can be null but is not expected to contain null elements)
results
)

Return a unique int result from the given Collection.

public static long

Returns:

the unique long result
longResult
(Collection<?>
the result Collection (can be null but is not expected to contain null elements)
results
)

Return a unique long result from the given Collection.

public static <T> T

Returns:

the single result object
nullableSingleResult
(Collection<T>
the result Collection (can be null and is also expected to contain null elements)
results
)

Return a single result object from the given Collection.

public static <T> T

Returns:

the unique result object
objectResult
(Collection<?>
the result Collection (can be null but is not expected to contain null elements)
results
,
Class<T> requiredType)

Return a unique result object from the given Collection.

public static <T> Optional<T>

Returns:

the single optional result object, or Optional.empty() if none
optionalResult
(Collection<T>
the result Collection (can be null)
results
)

Return a single result object from the given Collection.

public static <T> Optional<T>

Returns:

the single optional result object, or Optional.empty() if none
optionalResult
(Stream<T>
the result Stream (can be null)
results
)

Return a single result object from the given Stream.

public static <T> Optional<T>

Returns:

the single optional result object, or Optional.empty() if none
optionalResult
(Iterator<T>
the result Iterator (can be null)
results
)

Return a single result object from the given Iterator.

public static <T> T

Returns:

the single result object
requiredSingleResult
(Collection<T>
the result Collection (can be null but is not expected to contain null elements)
results
)

Return a single result object from the given Collection.

public static <T> T

Returns:

the unique result object
requiredUniqueResult
(Collection<T>
the result Collection (can be null but is not expected to contain null elements)
results
)

Return a unique result object from the given Collection.

public static <T> T

Returns:

the single result object, or null if none
singleResult
(Collection<T>
the result Collection (can be null)
results
)

Return a single result object from the given Collection.

public static <T> T

Returns:

the single result object, or null if none
singleResult
(Stream<T>
the result Stream (can be null)
results
)

Return a single result object from the given Stream.

public static <T> T

Returns:

the single result object, or null if none
singleResult
(Iterator<T>
the result Iterator (can be null)
results
)

Return a single result object from the given Iterator.

public static RuntimeException

Returns:

a translated persistence exception if translation is possible, or the raw exception if it is not
translateIfNecessary
(RuntimeException
an exception that we may wish to translate
rawException
,
PersistenceExceptionTranslator
the PersistenceExceptionTranslator to use to perform the translation
pet
)

Return a translated exception if this is appropriate, otherwise return the given exception as-is.

public static <T> T

Returns:

the unique result object, or null if none
uniqueResult
(Collection<T>
the result Collection (can be null)
results
)

Return a unique result object from the given Collection.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DataAccessUtilsback to summary
public DataAccessUtils()

Method Detail

intResultback to summary
public static int intResult(Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException

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 convertible to an int.

Parameters
results:Collection<?>

the result Collection (can be null but is not expected to contain null elements)

Returns:int

the unique int result

Exceptions
IncorrectResultSizeDataAccessException:
if more than one result object has been found in the given Collection
TypeMismatchDataAccessException:
if the unique object in the collection is not convertible to an int
EmptyResultDataAccessException:
if no result object at all has been found in the given Collection
longResultback to summary
public static long longResult(Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException

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 convertible to a long.

Parameters
results:Collection<?>

the result Collection (can be null but is not expected to contain null elements)

Returns:long

the unique long result

Exceptions
IncorrectResultSizeDataAccessException:
if more than one result object has been found in the given Collection
TypeMismatchDataAccessException:
if the unique object in the collection is not convertible to a long
EmptyResultDataAccessException:
if no result object at all has been found in the given Collection
nullableSingleResultback to summary
public static <T> T nullableSingleResult(Collection<T> results) throws IncorrectResultSizeDataAccessException

Return a single result object from the given Collection.

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

Parameters
results:Collection<T>

the result Collection (can be null and is also expected to contain null elements)

Returns:T

the single result object

Annotations
@Nullable
Exceptions
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
Since
5.0.2
objectResultback to summary
public static <T> T objectResult(Collection<?> results, Class<T> requiredType) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException

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 convertible to the specified required type.

Parameters
results:Collection<?>

the result Collection (can be null but is not expected to contain null elements)

Returns:T

the unique result object

Annotations
@SuppressWarnings:unchecked
Exceptions
IncorrectResultSizeDataAccessException:
if more than one result object has been found in the given Collection
TypeMismatchDataAccessException:
if the unique object does not match the specified required type
EmptyResultDataAccessException:
if no result object at all has been found in the given Collection
optionalResultback to summary
public static <T> Optional<T> optionalResult(Collection<T> results) throws IncorrectResultSizeDataAccessException

Return a single result object from the given Collection.

Returns Optional.empty() if 0 result objects found; throws an exception if more than 1 element found.

Parameters
results:Collection<T>

the result Collection (can be null)

Returns:Optional<T>

the single optional result object, or Optional.empty() if none

Exceptions
IncorrectResultSizeDataAccessException:
if more than one element has been found in the given Collection
Since
6.1
optionalResultback to summary
public static <T> Optional<T> optionalResult(Stream<T> results) throws IncorrectResultSizeDataAccessException

Return a single result object from the given Stream.

Returns Optional.empty() if 0 result objects found; throws an exception if more than 1 element found.

Parameters
results:Stream<T>

the result Stream (can be null)

Returns:Optional<T>

the single optional result object, or Optional.empty() if none

Exceptions
IncorrectResultSizeDataAccessException:
if more than one element has been found in the given Stream
Since
6.1
optionalResultback to summary
public static <T> Optional<T> optionalResult(Iterator<T> results) throws IncorrectResultSizeDataAccessException

Return a single result object from the given Iterator.

Returns Optional.empty() if 0 result objects found; throws an exception if more than 1 element found.

Parameters
results:Iterator<T>

the result Iterator (can be null)

Returns:Optional<T>

the single optional result object, or Optional.empty() if none

Exceptions
IncorrectResultSizeDataAccessException:
if more than one element has been found in the given Iterator
Since
6.1
requiredSingleResultback to summary
public static <T> T requiredSingleResult(Collection<T> results) throws IncorrectResultSizeDataAccessException

Return a single result object from the given Collection.

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

Parameters
results:Collection<T>

the result Collection (can be null but is not expected to contain null elements)

Returns:T

the single result object

Exceptions
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
requiredUniqueResultback to summary
public static <T> T requiredUniqueResult(Collection<T> results) throws IncorrectResultSizeDataAccessException

Return a unique result object from the given Collection.

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

Parameters
results:Collection<T>

the result Collection (can be null but is not expected to contain null elements)

Returns:T

the unique result object

Exceptions
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
See Also
org.springframework.util.CollectionUtils#hasUniqueObject
singleResultback to summary
public static <T> T singleResult(Collection<T> results) throws IncorrectResultSizeDataAccessException

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:Collection<T>

the result Collection (can be null)

Returns:T

the single result object, or null if none

Annotations
@Nullable
Exceptions
IncorrectResultSizeDataAccessException:
if more than one element has been found in the given Collection
singleResultback to summary
public static <T> T singleResult(Stream<T> results) throws IncorrectResultSizeDataAccessException

Return a single result object from the given Stream.

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

Parameters
results:Stream<T>

the result Stream (can be null)

Returns:T

the single result object, or null if none

Annotations
@Nullable
Exceptions
IncorrectResultSizeDataAccessException:
if more than one element has been found in the given Stream
Since
6.1
singleResultback to summary
public static <T> T singleResult(Iterator<T> results) throws IncorrectResultSizeDataAccessException

Return a single result object from the given Iterator.

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

Parameters
results:Iterator<T>

the result Iterator (can be null)

Returns:T

the single result object, or null if none

Annotations
@Nullable
Exceptions
IncorrectResultSizeDataAccessException:
if more than one element has been found in the given Iterator
Since
6.1
translateIfNecessaryback to summary
public static RuntimeException translateIfNecessary(RuntimeException rawException, PersistenceExceptionTranslator pet)

Return a translated exception if this is appropriate, otherwise return the given exception as-is.

Parameters
rawException:RuntimeException

an exception that we may wish to translate

pet:PersistenceExceptionTranslator

the PersistenceExceptionTranslator to use to perform the translation

Returns:RuntimeException

a translated persistence exception if translation is possible, or the raw exception if it is not

uniqueResultback to summary
public static <T> T uniqueResult(Collection<T> results) throws IncorrectResultSizeDataAccessException

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:Collection<T>

the result Collection (can be null)

Returns:T

the unique result object, or null if none

Annotations
@Nullable
Exceptions
IncorrectResultSizeDataAccessException:
if more than one result object has been found in the given Collection
See Also
org.springframework.util.CollectionUtils#hasUniqueObject