Class DataAccessUtils
Useful with any data access technology.
- Since:
- 1.0.2
- Author:
- Juergen Hoeller
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic intintResult(Collection<?> results) Return a unique int result from the given Collection.static longlongResult(Collection<?> results) Return a unique long result from the given Collection.static <T> TnullableSingleResult(Collection<T> results) Return a single result object from the given Collection.static <T> TobjectResult(Collection<?> results, Class<T> requiredType) Return a unique result object from the given Collection.static <T> Optional<T>optionalResult(Collection<T> results) Return a single result object from the given Collection.static <T> Optional<T>optionalResult(Iterator<T> results) Return a single result object from the given Iterator.static <T> Optional<T>optionalResult(Stream<T> results) Return a single result object from the given Stream.static <T> TrequiredSingleResult(Collection<T> results) Return a single result object from the given Collection.static <T> TrequiredUniqueResult(Collection<T> results) Return a unique result object from the given Collection.static <T> TsingleResult(Collection<T> results) Return a single result object from the given Collection.static <T> TsingleResult(Iterator<T> results) Return a single result object from the given Iterator.static <T> TsingleResult(Stream<T> results) Return a single result object from the given Stream.static RuntimeExceptiontranslateIfNecessary(RuntimeException rawException, PersistenceExceptionTranslator pet) Return a translated exception if this is appropriate, otherwise return the given exception as-is.static <T> TuniqueResult(Collection<T> results) Return a unique result object from the given Collection.
- 
Constructor Details- 
DataAccessUtilspublic DataAccessUtils()
 
- 
- 
Method Details- 
singleResult@Nullable public static <T> T singleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException Return a single result object from the given Collection.Returns nullif 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 nullif none
- Throws:
- IncorrectResultSizeDataAccessException- if more than one element has been found in the given Collection
 
- 
singleResult@Nullable public static <T> T singleResult(@Nullable Stream<T> results) throws IncorrectResultSizeDataAccessException Return a single result object from the given Stream.Returns nullif 0 result objects found; throws an exception if more than 1 element found.- Parameters:
- results- the result Stream (can be- null)
- Returns:
- the single result object, or nullif none
- Throws:
- IncorrectResultSizeDataAccessException- if more than one element has been found in the given Stream
- Since:
- 6.1
 
- 
singleResult@Nullable public static <T> T singleResult(@Nullable Iterator<T> results) throws IncorrectResultSizeDataAccessException Return a single result object from the given Iterator.Returns nullif 0 result objects found; throws an exception if more than 1 element found.- Parameters:
- results- the result Iterator (can be- null)
- Returns:
- the single result object, or nullif none
- Throws:
- IncorrectResultSizeDataAccessException- if more than one element has been found in the given Iterator
- Since:
- 6.1
 
- 
optionalResultpublic static <T> Optional<T> optionalResult(@Nullable 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- the result Collection (can be- null)
- Returns:
- the single optional result object, or Optional.empty()if none
- Throws:
- IncorrectResultSizeDataAccessException- if more than one element has been found in the given Collection
- Since:
- 6.1
 
- 
optionalResultpublic static <T> Optional<T> optionalResult(@Nullable 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- the result Stream (can be- null)
- Returns:
- the single optional result object, or Optional.empty()if none
- Throws:
- IncorrectResultSizeDataAccessException- if more than one element has been found in the given Stream
- Since:
- 6.1
 
- 
optionalResultpublic static <T> Optional<T> optionalResult(@Nullable 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- the result Iterator (can be- null)
- Returns:
- the single optional result object, or Optional.empty()if none
- Throws:
- IncorrectResultSizeDataAccessException- if more than one element has been found in the given Iterator
- Since:
- 6.1
 
- 
requiredSingleResultpublic static <T> T requiredSingleResult(@Nullable 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- the result Collection (can be- nullbut is not expected to contain- nullelements)
- 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
 
- 
nullableSingleResult@Nullable public static <T> T nullableSingleResult(@Nullable 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- the result Collection (can be- nulland is also expected to contain- nullelements)
- 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
- Since:
- 5.0.2
 
- 
uniqueResult@Nullable public static <T> T uniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException Return a unique result object from the given Collection.Returns nullif 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 nullif none
- Throws:
- IncorrectResultSizeDataAccessException- if more than one result object has been found in the given Collection
- See Also:
 
- 
requiredUniqueResultpublic static <T> T requiredUniqueResult(@Nullable 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- the result Collection (can be- nullbut is not expected to contain- nullelements)
- 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
- See Also:
 
- 
objectResultpublic static <T> T objectResult(@Nullable Collection<?> results, @Nullable 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- the result Collection (can be- nullbut is not expected to contain- nullelements)
- 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
 
- 
intResultpublic static int intResult(@Nullable 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- the result Collection (can be- nullbut is not expected to contain- nullelements)
- 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 convertible to an int
 
- 
longResultpublic static long longResult(@Nullable 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- the result Collection (can be- nullbut is not expected to contain- nullelements)
- 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 convertible to a long
 
- 
translateIfNecessarypublic static RuntimeException translateIfNecessary(RuntimeException rawException, PersistenceExceptionTranslator pet) Return a translated exception if this is appropriate, otherwise return the given exception as-is.- Parameters:
- rawException- an exception that we may wish to translate
- pet- the PersistenceExceptionTranslator to use to perform the translation
- Returns:
- a translated persistence exception if translation is possible, or the raw exception if it is not
 
 
-