Spring @Transactional调用抽象超类方法抛出org.hibernate.HibernateException

时间:2017-02-17 20:10:10

标签: spring hibernate spring-transactions transactional

我已经阅读了与此相关的stackoverflow上的20多个问题,但没有一个有效。

基本上我有一个带有3种方法的服务抽象超类。 Persist就像一个模板方法,而保存和更新只在需要时才被覆盖。

public abstract class BaseService<E> implements ServiceImpl{
    protected E save(E object, Class<E> returnedClass){//save object}
    protected E update(E object){//update object}

    public boolean persist(E object) {
        //validation rules

        if(shouldSave) {
            save(object);
        }else {
            update(object);
        }
    }
}

然后我有了这个子类

@Service
@Transactional
public CompanyService extends BaseService<Company> {
    @Autowired
    private CompanyRepository repository;

    public void testSaving(Company company) {
        repository.save(company);
    }

    @Override //from service impl
    public CommpanyRepositoy getRepository() {
        return repository;
    }
}

这是我的切入点

@RestController
@RequestMapping("/company")
public CompanyRestController {
    @Autowired
    private CompanyService service;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public boolean save(@RequestBody Company company) {
        return service.persist(company);
    }
}

现在,问题是,如果我呼吁使用testSaving方法,一切顺利。但是,当我调用persist方法时,它总是会提到错误。我尝试过很多东西,比如将@Transactional添加到BaseService超类中,甚至将它添加到我的存储库中也无法正常工作。拜托,我需要解决这个问题,它让我疯狂。有什么建议吗?

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
    at br.com.jeffersontpadua.spring.generic.BaseRepository.getSession(BaseRepository.java:56)
    at br.com.jeffersontpadua.spring.generic.BaseRepository.save(BaseRepository.java:27)
    at br.com.jeffersontpadua.spring.service.BaseService.save(BaseService.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:723)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
    at br.com.jeffersontpadua.spring.service.CompanyService$$EnhancerBySpringCGLIB$$a3329725.save(<generated>)
    at br.com.jeffersontpadua.spring.service.BaseService.persist(BaseService.java:34)
    at br.com.jeffersontpadua.spring.rest.CompanyRestController.save(CompanyRestController.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at br.com.jeffersontpadua.spring.filter.RequestFilter.doFilter(RequestFilter.java:34)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:2)

这是故意的,请参阅SPR-7824

您基本上需要将@Transactional注释放在超类而不是子类上,这将导致所有子类方法都是事务性的以及超类上的那些。

如果你不能这样做,请覆盖子类中的方法并委托给超类。

答案 1 :(得分:1)

我最终找到了实现这一目标的方法,因此,对于仍在努力解决此问题的人来说,诀窍是将@Transactional(propagation = Propagation.REQUIRES_NEW)添加到您的超类xD