/** * Handle the case where the ResultSet contains multiple rows. * @throws DataAccessException a corresponding exception, * by default an IncorrectResultSizeDataAccessException * @see org.springframework.dao.IncorrectResultSizeDataAccessException */ protected void handleMultipleRowsFound() throws DataAccessException { throw new IncorrectResultSizeDataAccessException( "LobStreamingResultSetExtractor found multiple rows in database", 1); }
/** * Return a single result object from the given Collection. * <p>Returns {@code null} if 0 result objects found; * throws an exception if more than 1 element found. * @param results the result Collection (can be {@code null}) * @return the single result object, or {@code null} if none * @throws IncorrectResultSizeDataAccessException if more than one * element has been found in the given Collection */ @Nullable public static <T> T singleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { return null; } if (results.size() > 1) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
/** * Return a single result object from the given Collection. * <p>Throws an exception if 0 or more than 1 element found. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the single result object * @throws IncorrectResultSizeDataAccessException if more than one * element has been found in the given Collection * @throws EmptyResultDataAccessException if no element at all * has been found in the given Collection */ public static <T> T requiredSingleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { throw new EmptyResultDataAccessException(1); } if (results.size() > 1) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
/** * Return a unique result object from the given Collection. * <p>Returns {@code null} if 0 result objects found; * throws an exception if more than 1 instance found. * @param results the result Collection (can be {@code null}) * @return the unique result object, or {@code null} if none * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @see org.springframework.util.CollectionUtils#hasUniqueObject */ @Nullable public static <T> T uniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { return null; } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
/** * Return a single result object from the given Collection. * <p>Throws an exception if 0 or more than 1 element found. * @param results the result Collection (can be {@code null} * and is also expected to contain {@code null} elements) * @return the single result object * @throws IncorrectResultSizeDataAccessException if more than one * element has been found in the given Collection * @throws EmptyResultDataAccessException if no element at all * has been found in the given Collection * @since 5.0.2 */ @Nullable public static <T> T nullableSingleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { // This is identical to the requiredSingleResult implementation but differs in the // semantics of the incoming Collection (which we currently can't formally express) if (CollectionUtils.isEmpty(results)) { throw new EmptyResultDataAccessException(1); } if (results.size() > 1) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
@Override public ScimGroup getByName(String displayName, String zoneId) { if (!hasText(displayName)) { throw new IncorrectResultSizeDataAccessException("group name must contain text", 1, 0); } List<ScimGroup> groups = jdbcTemplate.query(GET_GROUP_BY_NAME_SQL, rowMapper, displayName, zoneId); if (groups.size()==1) { return groups.get(0); } else { throw new IncorrectResultSizeDataAccessException("Invalid result size found for:"+displayName, 1, groups.size()); } }
/** * Return a unique result object from the given Collection. * <p>Throws an exception if 0 or more than 1 instance found. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique result object * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @throws EmptyResultDataAccessException if no result object at all * has been found in the given Collection * @see org.springframework.util.CollectionUtils#hasUniqueObject */ public static <T> T requiredUniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { throw new EmptyResultDataAccessException(1); } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
public boolean userExists(String username) { List<String> users = getJdbcTemplate().queryForList(userExistsSql, new String[] { username }, String.class); if (users.size() > 1) { throw new IncorrectResultSizeDataAccessException( "More than one user found with name '" + username + "'", 1); } return users.size() == 1; }
public IdentityProvider retrieveByIssuer(String issuer, String zoneId) throws IncorrectResultSizeDataAccessException { List<IdentityProvider> providers = retrieveAll(true, zoneId) .stream() .filter(p -> OIDC10.equals(p.getType()) && issuer.equals(((OIDCIdentityProviderDefinition) p.getConfig()).getIssuer())) .collect(Collectors.toList()); if (providers.isEmpty()) { throw new IncorrectResultSizeDataAccessException(String.format("Active provider with issuer[%s] not found", issuer), 1); } else if (providers.size() > 1) { throw new IncorrectResultSizeDataAccessException(String.format("Duplicate providers with issuer[%s] not found", issuer), 1); } return providers.get(0); }
public boolean userExists(String username) { List<String> users = getJdbcTemplate().queryForList(userExistsSql, new String[] { username }, String.class); if (users.size() > 1) { throw new IncorrectResultSizeDataAccessException( "More than one user found with name '" + username + "'", 1); } return users.size() == 1; }
@Override public UaaUser retrieveUserByEmail(String email, String origin) throws UsernameNotFoundException { String sql = isCaseInsensitive() ? DEFAULT_CASE_INSENSITIVE_USER_BY_EMAIL_AND_ORIGIN_QUERY : DEFAULT_CASE_SENSITIVE_USER_BY_EMAIL_AND_ORIGIN_QUERY; List<UaaUser> results = jdbcTemplate.query(sql, mapper, email.toLowerCase(Locale.US), true, origin, IdentityZoneHolder.get().getId()); if(results.size() == 0) { return null; } else if(results.size() == 1) { return results.get(0); } else { throw new IncorrectResultSizeDataAccessException(String.format("Multiple users match email=%s origin=%s", email, origin), 1, results.size()); } }
@Override public Optional<T> findOne(Predicate predicate) { Assert.notNull(predicate, "Predicate must not be null!"); try { return Optional.ofNullable(createQueryFor(predicate).fetchOne()); } catch (NonUniqueResultException ex) { throw new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex); } }
@Override public <S extends T> Optional<S> findOne(Example<S> example) { RedisOperationChain operationChain = createQuery(example); KeyValueQuery<RedisOperationChain> query = new KeyValueQuery<>(operationChain); Iterator<T> iterator = keyValueTemplate.find(query.limit(2), entityInformation.getJavaType()).iterator(); Optional result = Optional.empty(); if (iterator.hasNext()) { result = Optional.of((S) iterator.next()); if (iterator.hasNext()) { throw new IncorrectResultSizeDataAccessException(1); } } return result; }
@Override public T oneValue() { List<T> result = doFind(new DelegatingQueryCursorPreparer(getCursorPreparer(query, null)).limit(2)); if (ObjectUtils.isEmpty(result)) { return null; } if (result.size() > 1) { throw new IncorrectResultSizeDataAccessException("Query " + asString() + " returned non unique result.", 1); } return result.iterator().next(); }
return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
@Override public Optional<T> findOne(Predicate predicate) { try { return Optional.ofNullable(createQuery(predicate).select(path).fetchOne()); } catch (NonUniqueResultException ex) { throw new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex); } }
@Override public Optional<T> findOne(Predicate predicate) { try { return Optional.ofNullable(createQuery(predicate).select(path).fetchOne()); } catch (NonUniqueResultException ex) { throw new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex); } }
@Override public <S extends T> Mono<S> findOne(Example<S> example) { Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); q.limit(2); return mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName()).buffer(2) .map(vals -> { if (vals.size() > 1) { throw new IncorrectResultSizeDataAccessException(1); } return vals.iterator().next(); }).next(); }
return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);