According to the doc an @Repository annotation is required to activate Exception Translation:
§2.1.1 "Beyond that it activates persistence exception translation for all beans annotated with @Repository to let exceptions being thrown by the JPA presistence providers be converted into Spring's
DataAccessException hierarchy."
When I try this with a mini-example, it translates the exceptions even without @Repository:
Code:import org.springframework.data.jpa.repository.JpaRepository; public interface CustomerTestRepository extends JpaRepository<Customer, Long> { }Code:<jpa:repositories base-package="...jpa.test" /> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- just basic entityManagerFactory, transactionManager, ... -->
The following test fails:
I also tried to fake the exception translator with:Code:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath*:test-root-context.xml" }) public class SpringDataExceptionTranslationTest { @Autowired CustomerTestRepository cr; @Test public void testOptimisticLockingException() { try { Customer c = cr.save(new Customer("a", "Hans", "Muster", 1.3)); c.setVersion(c.getVersion() - 1); // try provoking an optimstic locking exception cr.save(c); Assert.fail("should have thrown an exception"); } catch (OptimisticLockingFailureException dae) { Assert.fail("should not throw a SPRING optimistic locking exception"); } catch (OptimisticLockException ole) { // expected } } }
But it dit not help either.Code:<bean id="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0" class="java.lang.String" />


Reply With Quote
