/** * Generic object finder method, used by all other {@code findObject} methods. * Object finder methods are like EJB entity bean finders, in that it is * considered an error if they return more than one result. * @return the result object, or {@code null} if not found. Subclasses may * choose to treat this as an error and throw an exception. * @see org.springframework.dao.support.DataAccessUtils#singleResult */ @Nullable public T findObject(@Nullable Object[] params, @Nullable Map<?, ?> context) throws DataAccessException { List<T> results = execute(params, context); return DataAccessUtils.singleResult(results); }
@Test public void withLong() { Collection<Long> col = new HashSet<>(1); col.add(5L); assertEquals(Long.valueOf(5L), DataAccessUtils.uniqueResult(col)); assertEquals(Long.valueOf(5L), DataAccessUtils.requiredUniqueResult(col)); assertEquals(Long.valueOf(5L), DataAccessUtils.objectResult(col, Long.class)); assertEquals("5", DataAccessUtils.objectResult(col, String.class)); assertEquals(5, DataAccessUtils.intResult(col)); assertEquals(5, DataAccessUtils.longResult(col)); }
@Override public Object invoke(MethodInvocation mi) throws Throwable { try { return mi.proceed(); } catch (RuntimeException ex) { // Let it throw raw if the type of the exception is on the throws clause of the method. if (!this.alwaysTranslate && ReflectionUtils.declaresException(mi.getMethod(), ex.getClass())) { throw ex; } else { PersistenceExceptionTranslator translator = this.persistenceExceptionTranslator; if (translator == null) { Assert.state(this.beanFactory != null, "No PersistenceExceptionTranslator set"); translator = detectPersistenceExceptionTranslators(this.beanFactory); this.persistenceExceptionTranslator = translator; } throw DataAccessUtils.translateIfNecessary(ex, translator); } } }
@Test @SuppressWarnings("deprecation") // on JDK 9 public void withEquivalentIntegerInstanceTwice() { Collection<Integer> col = new ArrayList<>(2); col.add(new Integer(5)); col.add(new Integer(5)); try { DataAccessUtils.uniqueResult(col); fail("Should have thrown IncorrectResultSizeDataAccessException"); } catch (IncorrectResultSizeDataAccessException ex) { // expected assertEquals(1, ex.getExpectedSize()); assertEquals(2, ex.getActualSize()); } }
private void doDataSourceHealthCheck(Health.Builder builder) throws Exception { String product = getProduct(); builder.up().withDetail("database", product); String validationQuery = getValidationQuery(product); if (StringUtils.hasText(validationQuery)) { // Avoid calling getObject as it breaks MySQL on Java 7 List<Object> results = this.jdbcTemplate.query(validationQuery, new SingleColumnRowMapper()); Object result = DataAccessUtils.requiredSingleResult(results); builder.withDetail("hello", result); } }
/** * 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. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique int result * @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 * @throws TypeMismatchDataAccessException if the unique object * in the collection is not convertible to an int */ public static int intResult(@Nullable Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { return objectResult(results, Number.class).intValue(); }
throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { Object result = requiredUniqueResult(results); if (requiredType != null && !requiredType.isInstance(result)) { if (String.class == requiredType) {
/** * Returned {@link Map} will have values of {@link String} or a * {@link List} of {@link String}. * * @see org.jasig.portal.services.persondir.IPersonAttributeDao#getUserAttributes(java.util.Map) */ public Map parseAttributeMapFromResults(final List queryResults) { final Map uniqueResult = (Map)DataAccessUtils.uniqueResult(queryResults); //If it's null no user was found, correct behavior is to return null return uniqueResult; }
public <T> T queryForObject(String sql, RowMapper<T> rowMapper) throws DataAccessException { List<T> results = query(sql, rowMapper); return DataAccessUtils.requiredSingleResult(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 convertible to a long. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique long result * @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 * @throws TypeMismatchDataAccessException if the unique object * in the collection is not convertible to a long */ public static long longResult(@Nullable Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { return objectResult(results, Number.class).longValue(); }
throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { Object result = requiredUniqueResult(results); if (requiredType != null && !requiredType.isInstance(result)) { if (String.class == requiredType) {
/** * Generic object finder method for named parameters. * @param paramMap a Map of parameter name to parameter object, * matching named parameters specified in the SQL statement. * Ordering is not significant. * @param context contextual information passed to the {@code mapRow} * callback method. The JDBC operation itself doesn't rely on this parameter, * but it can be useful for creating the objects of the result list. * @return a List of objects, one per row of the ResultSet. Normally all these * will be of the same class, although it is possible to use different types. */ @Nullable public T findObjectByNamedParam(Map<String, ?> paramMap, @Nullable Map<?, ?> context) throws DataAccessException { List<T> results = executeByNamedParam(paramMap, context); return DataAccessUtils.singleResult(results); }
@Test public void withInteger() { Collection<Integer> col = new HashSet<>(1); col.add(5); assertEquals(Integer.valueOf(5), DataAccessUtils.uniqueResult(col)); assertEquals(Integer.valueOf(5), DataAccessUtils.requiredUniqueResult(col)); assertEquals(Integer.valueOf(5), DataAccessUtils.objectResult(col, Integer.class)); assertEquals("5", DataAccessUtils.objectResult(col, String.class)); assertEquals(5, DataAccessUtils.intResult(col)); assertEquals(5, DataAccessUtils.longResult(col)); }
public <T> T optionalUniqueResult(List<T> results, T zeroResult) { T result = DataAccessUtils.uniqueResult(results); if (result == null) return zeroResult; return result; }
@Override public void flush() { try { getEntityManagerHolder().getEntityManager().flush(); } catch (RuntimeException ex) { throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect()); } }
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper) throws DataAccessException { List<T> results = query(sql, args, argTypes, new RowMapperResultSetExtractor<T>(rowMapper, 1)); return DataAccessUtils.requiredSingleResult(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 convertible to a long. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique long result * @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 * @throws TypeMismatchDataAccessException if the unique object * in the collection is not convertible to a long */ public static long longResult(@Nullable Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { return objectResult(results, Number.class).longValue(); }
throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { Object result = requiredUniqueResult(results); if (requiredType != null && !requiredType.isInstance(result)) { if (String.class.equals(requiredType)) {
private Optional<String> selectForUpdate(String target) { return Optional.ofNullable(DataAccessUtils.singleResult(jdbcTemplate.query(selectFromSagaLockTableSql, (rs, rowNum) -> { return rs.getString("saga_id"); }, target))); }
@Test public void withSameIntegerInstanceTwice() { Integer i = 5; Collection<Integer> col = new ArrayList<>(1); col.add(i); col.add(i); assertEquals(Integer.valueOf(5), DataAccessUtils.uniqueResult(col)); assertEquals(Integer.valueOf(5), DataAccessUtils.requiredUniqueResult(col)); assertEquals(Integer.valueOf(5), DataAccessUtils.objectResult(col, Integer.class)); assertEquals("5", DataAccessUtils.objectResult(col, String.class)); assertEquals(5, DataAccessUtils.intResult(col)); assertEquals(5, DataAccessUtils.longResult(col)); }