java - 事务内部的 Spring DAO 异常翻译

标签 java spring exception dao

我正在使用 Spring 3.1.2Hibernate 4

我有一个用 @Repository 注释的 DAO 实现类 MyDaoImpl 以便启用异常转换。我有一个用 @Transactional 注释的服务类 MyService 如下:

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);
    }
}

我写了一个单元测试类 MyServiceTest 如下:

@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
    }
}

当我运行测试时,我得到一个 Hibernate 特定的异常 ConstraintViolationException。我在论坛上发现这是因为翻译系统发生在交易之外,所以在这种情况下 testCreateA() 返回。我不知道这是否是真正的原因,但如果是,则意味着我无法测试翻译是否适用于我的 DAO。一种解决方案是从我的单元测试中删除 @Transactional 注释,但我不会从回滚功能中受益。

你有什么建议?


编辑:我已将在我的上下文中声明的 SessionFactory 添加到测试类中,以便我可以访问当前 session 以进行刷新。

一些额外的解释:在这种情况下,我在刷新 session (在事务内)时得到异常。我刷新 session 以避免误报,如文档中所述。此外,由于默认传播是必需的,testCreateA() 事务也用于调用 createA(),因此更改不会(通常)刷新,直到 testCreateA() 返回。

最佳答案

您是否添加了 PersistenceExceptionTranslationPostProcessor bean 定义?喜欢

   <!--
        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" />

来自 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

关于java - 事务内部的 Spring DAO 异常翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12140644/

相关文章:

java - 如何从不同的 Maven 项目运行 Eclipse 中的主类?

java - 如何检查 Dropbox 用户 session 是否有效?

java - 如何使用哈希表改进一系列数字的 2 和算法?

java - 重新创建springrabbitCachingConnectionFactory的所有连接

java - 是什么导致了 java.lang.ArrayIndexOutOfBoundsException 以及如何防止它?

java - 访问 webService 时出现 NullPointerException。我该如何解决它

java - spring 配置文件和 web.xml 的确切位置在哪里?

spring - Keycloak:作为 docker 服务运行时无效的 token 颁发者

exception - grails webflow 中的 BadlyFormattedFlowExecutionKeyException

java - 从用户那里获取偶数并在数字为奇数时给出异常错误