Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I enable Exception Translation in Spring-Data when running from a Test?
    primarykey
    data
    text
    <p>I am using Spring-Data and want to let <code>PersistenceExceptions</code> be translated to Springs <code>DataAccessExceptions</code>.</p> <p>I activated Spring-Data with <code>@EnableJpaRepositories</code> and I can see, that the <code>org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0</code> is loaded by Spring during startup. A <code>PersistenceExceptionTranslator</code> is available, too.</p> <p>As I understand the <a href="http://static.springsource.org/spring-data/data-jpa/docs/current/reference/htmlsingle/#jpa.introduction" rel="nofollow">documentation</a> the namespace declaration <code>&lt;jpa:repositories&gt;</code> activates persistence exception translation for Beans annotated with <code>@Repository</code>. Is this the case for Java Configuration (<code>@EnableJpaRepositories</code>) as well?</p> <p>Does this mean, I have to annotate my Spring-Data repository interface with <code>@Repository</code> or is it optional using Spring Data as configured like mentioned above?</p> <p>This is the interface:</p> <pre><code>@Repository public interface DemoDao extends JpaRepository&lt;Demo, Long&gt; {} </code></pre> <p>And this is the test:</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {TestDaoWithEmbeddedDatabaseConfiguration.class}) @Transactional public class DemoTest { @Test(expected = DataAccessException.class) public void testFindByTransactionStatus() throws Exception { persistDemoDataWithUniqueConstraintError(); // this calls DemoDao#save() } } </code></pre> <p>This generates:</p> <pre><code>javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException </code></pre> <p>I cannot figure out how to enable the exception translation. Any ideas?</p> <p><strong>Edit 1:</strong></p> <p>This is the test configuration and Spring setup:</p> <pre><code>@Configuration @ComponentScan("com.demo.dao") @EnableJpaRepositories("com.demo.dao") @EnableTransactionManagement public class TestDaoWithEmbeddedDatabaseConfiguration { // DataSource (EmbeddedDatabaseBuilder), EntityManagerFactory @Bean configuration @Bean public PersistenceExceptionTranslator persistenceExceptionTranslator() { return new HibernateExceptionTranslator(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload