如何从相同的dao方法中正确调用dao方法

时间:2014-10-21 09:43:01

标签: java spring hibernate jpa

我有2道。

@EnableTransactionManagement()
@Repository
public class TTTestFirstImpl  implements TTestFirstDao {

    protected EntityManager entityManager;
    ...

    @Autowired
    public TTestSecondDao secondDao;

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void first() {
        entityManager.persist(new TableTest1().setName("first dao persist"));

        try {
              secondDao.second();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

而且第二个dao看起来像这样:

@EnableTransactionManagement()
@Repository
public class TTTestSecondImpl implements TTestSecondDao {

    protected EntityManager entityManager;
    ...

    @Override
    @Transactional(propagation=Propagation.REQUIRES)
    public void second() {
        entityManager.persist(...);
        throw new RuntimeException(); // to roll back
    }

第一种和第二种方法在DB中保留数据。在第一道我注入了第二道。 我从second()方法调用first()方法。

一切都很顺利,因为second()方法有Propagation.REQUIRES所有持久化数据都是滚动支持(从第一个()和第二个()方法持久化)。 DB中没有数据。 非常好!

现在我在First Dao Implementation中创建了third()方法,类似于second() mehtod。唯一的区别是third()类中的TTEstFirstImpl方法ID。

现在我的班级看起来像这样:

@EnableTransactionManagement()
@Repository
public class TTTestFirstImpl  implements TTestFirstDao {


    @Autowired
    private ApplicationContext applicationContext;

    private TTestFirstDao getSpringProxy() {
        return applicationContext.getBean(this.getClass());
    }

    @Transactional(propagation=Propagation.REQUIRED)
    public void third() {

        entityManager.persist(...);
        throw new RuntimeException();

    }


    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void first() {
        entityManager.persist(...);

        try {
            //  dao.second();
            getSpringProxy().third();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

当我调用具有third()的{​​{1}}方法时,我没有相同的结果。仅支持Propagation.REQUIRED个方法。但由于我有third(),因此Propagation.REQUIRED 的持久数据也必须支持滚动。

如果我在没有getSpringProxy()的情况下调用first(),它根本不会回滚。这对我来说也不清楚。

p.s我在DB中吮吸了我的配置:

third()

0 个答案:

没有答案
相关问题