简体   繁体   中英

Spring DAO exception translation inside transaction

I'm using Spring 3.1.2 with Hibernate 4 .

I have a DAO implementation class MyDaoImpl annotated with @Repository so that exception translation is enabled. I have a service class MyService annotated with @Transactional as follows:

public class MyService implements IMyService {

    private MyDao myDao;

    @Autowired
    public void setMyDao(MyDao dao) {
       this.myDao = dao;
    }

    @Override
    @Transactional
    public void createA(String name)
    {
        A newA = new A(name);
        this.myDao.saveA(newA);
    }
}

I've wrote a unit tests class MyServiceTest as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:beans.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class MyServiceTest implements IMyServiceTest {

    private IMyService myService;

    private SessionFactory sessionFactory;

    @Autowired
    public void setMyService(IMyService myService)
    {
        this.myService = myService;
    }

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }

    @Test
    @Override
    public void testCreateA()
    {
       //Assume that there is already a row of table A with the name "A1"
       //so I expect to get a Spring DataAccessException (or subtypes) 
       //when the session is flushed
       this.myService.createA("A1");

       this.sessionFactory.getCurrentSession().flush();

       //asserts
    }
}

When I run the test, I get a Hibernate specific exception ConstraintViolationException . I've found on the forum that this is because the translation system takes place outside the transaction, so in this case after testCreateA() returns. I don't know if this is the real cause, but if it is, it means that I can't test that the translation works for my DAOs. One solution would be to remove the @Transactional annotations from my unit tests, but I would no benefit from the rollback feature.

What are your recommendations?


EDIT: I've added the SessionFactory declared in my context to the test class, so that I can access the current session for flushing.

Some additional explanations: In this case, I get the exception when the session is flushed (which is inside the transaction). I flush the session in order to avoid false positives as it is explained in the docs. Also, since the default propagation is REQUIRED, the testCreateA() transaction is also used for the call to createA() , so the changes are not flushed (generally) until testCreateA() returns.

Have you added PersistenceExceptionTranslationPostProcessor bean defination? Like

   <!--
        Post-processor to perform exception translation on @Repository classes
        (from native exceptions such as JPA PersistenceExceptions to
        Spring's DataAccessException hierarchy).
    -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

From Spring doc .

Bean post-processor that automatically applies persistence exception translation to any bean that carries the @Repository annotation, adding a corresponding PersistenceExceptionTranslationAdvisor to the exposed proxy (either an existing AOP proxy or a newly generated proxy that implements all of the target's interfaces).

Translates native resource exceptions to Spring's DataAccessException hierarchy. Autodetects beans that implement the PersistenceExceptionTranslator interface, which are subsequently asked to translate candidate exceptions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM