Get the data which is inserted using @Transactional in the same transaction

房东的猫 提交于 2019-12-22 01:24:28

问题


I am inserting the data in one method(has @Transactional(propagation = Propagation.Required)) but in the other method(has @Transactional(propagation = Propagation.Required)) if I try to get the same data it is giving null.

Both methods are wrote in service layer with @Transactional (rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)

How to get the data which is inserted in the same transaction. Something like :-

 @Service
    public class Service{
        @Transactional
        public void method(){
            mapper.insert();        //insert to DB(Using Mapper interface)
            ServiceLayer.method2()
        }
    }

@Service
public void ServiceLayer{

    @Transactional
    public static void method2(){
        result  = mapper.select() //Select inserted data - returning null
    }
}

回答1:


In order to persist the changes made to the current session you can invoke entityManager.flush();




回答2:


I found the answer...after removing @transactional from ServiceLayer.method2() it's worked fine.



来源:https://stackoverflow.com/questions/36132667/get-the-data-which-is-inserted-using-transactional-in-the-same-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!