@Override @Nullable public DataAccessException translateExceptionIfPossible(RuntimeException ex) { for (PersistenceExceptionTranslator pet : this.delegates) { DataAccessException translatedDex = pet.translateExceptionIfPossible(ex); if (translatedDex != null) { return translatedDex; } } return null; }
@Override public final void afterPropertiesSet() throws IllegalArgumentException, BeanInitializationException { // Let abstract subclasses check their configuration. checkDaoConfig(); // Let concrete implementations initialize themselves. try { initDao(); } catch (Exception ex) { throw new BeanInitializationException("Initialization of DAO failed", ex); } }
@Override @Nullable public <T> T queryForObject(String sql, RowMapper<T> rowMapper) throws DataAccessException { List<T> results = query(sql, rowMapper); return DataAccessUtils.nullableSingleResult(results); }
/** * 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 exceptionTranslationWithTranslation() { MapPersistenceExceptionTranslator mpet1 = new MapPersistenceExceptionTranslator(); RuntimeException in1 = new RuntimeException("in"); InvalidDataAccessApiUsageException out1 = new InvalidDataAccessApiUsageException("out"); InvalidDataAccessApiUsageException out2 = new InvalidDataAccessApiUsageException("out"); mpet1.addTranslation(in1, out1); ChainedPersistenceExceptionTranslator chainedPet1 = new ChainedPersistenceExceptionTranslator(); assertSame("Should not translate yet", in1, DataAccessUtils.translateIfNecessary(in1, chainedPet1)); chainedPet1.addDelegate(mpet1); assertSame("Should now translate", out1, DataAccessUtils.translateIfNecessary(in1, chainedPet1)); // Now add a new translator and verify it wins MapPersistenceExceptionTranslator mpet2 = new MapPersistenceExceptionTranslator(); mpet2.addTranslation(in1, out2); chainedPet1.addDelegate(mpet2); assertSame("Should still translate the same due to ordering", out1, DataAccessUtils.translateIfNecessary(in1, chainedPet1)); ChainedPersistenceExceptionTranslator chainedPet2 = new ChainedPersistenceExceptionTranslator(); chainedPet2.addDelegate(mpet2); chainedPet2.addDelegate(mpet1); assertSame("Should translate differently due to ordering", out2, DataAccessUtils.translateIfNecessary(in1, chainedPet2)); RuntimeException in2 = new RuntimeException("in2"); OptimisticLockingFailureException out3 = new OptimisticLockingFailureException("out2"); assertNull(chainedPet2.translateExceptionIfPossible(in2)); MapPersistenceExceptionTranslator mpet3 = new MapPersistenceExceptionTranslator(); mpet3.addTranslation(in2, out3); chainedPet2.addDelegate(mpet3); assertSame(out3, chainedPet2.translateExceptionIfPossible(in2)); }
@Test public void empty() { ChainedPersistenceExceptionTranslator pet = new ChainedPersistenceExceptionTranslator(); //MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator(); RuntimeException in = new RuntimeException("in"); assertSame(in, DataAccessUtils.translateIfNecessary(in, pet)); }
@Test public void exceptionTranslationWithTranslation() { MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator(); RuntimeException in = new RuntimeException("in"); InvalidDataAccessApiUsageException out = new InvalidDataAccessApiUsageException("out"); mpet.addTranslation(in, out); assertSame(out, DataAccessUtils.translateIfNecessary(in, mpet)); }
@Test public void exceptionTranslationWithNoTranslation() { MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator(); RuntimeException in = new RuntimeException(); assertSame(in, DataAccessUtils.translateIfNecessary(in, mpet)); }
/** * Create a new PersistenceExceptionTranslationAdvisor. * @param persistenceExceptionTranslator the PersistenceExceptionTranslator to use * @param repositoryAnnotationType the annotation type to check for */ public PersistenceExceptionTranslationAdvisor( PersistenceExceptionTranslator persistenceExceptionTranslator, Class<? extends Annotation> repositoryAnnotationType) { this.advice = new PersistenceExceptionTranslationInterceptor(persistenceExceptionTranslator); this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true); }
/** * 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(); }
protected RepositoryInterface createProxy(RepositoryInterfaceImpl target) { MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator(); mpet.addTranslation(persistenceException1, new InvalidDataAccessApiUsageException("", persistenceException1)); ProxyFactory pf = new ProxyFactory(target); pf.addInterface(RepositoryInterface.class); addPersistenceExceptionTranslation(pf, mpet); return (RepositoryInterface) pf.getProxy(); }
/** * 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); }
/** * Return a translated exception if this is appropriate, * otherwise return the given exception as-is. * @param rawException an exception that we may wish to translate * @param pet the PersistenceExceptionTranslator to use to perform the translation * @return a translated persistence exception if translation is possible, * or the raw exception if it is not */ public static RuntimeException translateIfNecessary( RuntimeException rawException, PersistenceExceptionTranslator pet) { Assert.notNull(pet, "PersistenceExceptionTranslator must not be null"); DataAccessException dae = pet.translateExceptionIfPossible(rawException); return (dae != null ? dae : rawException); }
/** * Create a new PersistenceExceptionTranslationAdvisor. * @param beanFactory the ListableBeanFactory to obtaining all * PersistenceExceptionTranslators from * @param repositoryAnnotationType the annotation type to check for */ PersistenceExceptionTranslationAdvisor( ListableBeanFactory beanFactory, Class<? extends Annotation> repositoryAnnotationType) { this.advice = new PersistenceExceptionTranslationInterceptor(beanFactory); this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true); }
/** * 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(); }
@Override @Nullable public <T> T queryForObject(String sql, RowMapper<T> rowMapper, @Nullable Object... args) throws DataAccessException { List<T> results = query(sql, args, new RowMapperResultSetExtractor<>(rowMapper, 1)); return DataAccessUtils.nullableSingleResult(results); }
private RuntimeException convertException(RuntimeException ex) { DataAccessException dae = (this.exceptionTranslator != null) ? this.exceptionTranslator.translateExceptionIfPossible(ex) : EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex); return (dae != null ? dae : ex); } }
@Override @Nullable public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper) throws DataAccessException { List<T> results = query(sql, args, argTypes, new RowMapperResultSetExtractor<>(rowMapper, 1)); return DataAccessUtils.nullableSingleResult(results); }
@Override @Nullable public <T> T queryForObject(String sql, @Nullable Object[] args, RowMapper<T> rowMapper) throws DataAccessException { List<T> results = query(sql, args, new RowMapperResultSetExtractor<>(rowMapper, 1)); return DataAccessUtils.nullableSingleResult(results); }
@Override @Nullable public <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper) throws DataAccessException { List<T> results = getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rowMapper); return DataAccessUtils.nullableSingleResult(results); }