I am using Spring -Data and want PersistenceExceptions be translated into Springs DataAccessExceptions .
I activated Spring-Data with @EnableJpaRepositories , and I see that org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0 loaded by Spring at startup. A PersistenceExceptionTranslator also available.
As I understand the documentation , the <jpa:repositories> namespace declaration activates Beans exception translation annotated using @Repository . @EnableJpaRepositories this also apply to Java configuration ( @EnableJpaRepositories )?
Does this mean that I should annotate my Spring -Data repository interface using @Repository or do I need to use Spring data as configured as above?
This is the interface:
@Repository public interface DemoDao extends JpaRepository<Demo, Long> {}
And this is the test:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {TestDaoWithEmbeddedDatabaseConfiguration.class}) @Transactional public class DemoTest { @Test(expected = DataAccessException.class) public void testFindByTransactionStatus() throws Exception { persistDemoDataWithUniqueConstraintError();
This generates:
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException
I cannot figure out how to enable exception translation. Any ideas?
Change 1:
This is a test configuration and Spring setup:
@Configuration @ComponentScan("com.demo.dao") @EnableJpaRepositories("com.demo.dao") @EnableTransactionManagement public class TestDaoWithEmbeddedDatabaseConfiguration {
source share