没有活动事务,保存无效 - Hibernate

时间:2015-01-23 14:11:36

标签: java spring hibernate postgresql jdbc

将条目保存到postgres DB时遇到一些问题。我正在使用hibernate和spring但是我无法为此目的使用DAO类。相反,以下代码适用于我:

Session session = sessionFactory.openSession();
    Transaction tx = null;
    Blob blob = null;

    try {
        tx = session.beginTransaction();
        VoiceMail message02 = new VoiceMail(44, 1, "dir/msgs",
                "dummy_data", "dummy_data", "6003", "19/01/2015",
                "1:35", "flag", "dummy_data", "dummy_data", blob,
                "label", false, "1");
        //voiceMailDao.save(message02);
        session.save(message02);
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }

当我尝试评论该行时

session.save(message02);

并离开

voiceMailDao.save(message02);

它不起作用。生成以下输出:

save is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
at com.sun.proxy.$Proxy750.save(Unknown Source)
at com.musala.ving.daoimpl.VoiceMailDaoImpl.save(VoiceMailDaoImpl.java:17)
at com.musala.ving.controllers.VoiceMailController.list(VoiceMailController.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
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:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

很抱歉这篇文章很长,但我在这里阅读了有关此问题的所有主题,但我仍然无法理解。

我的DAO课程如下:

public class VoiceMailDaoImpl  implements VoiceMailDao{

@Autowired
private SessionFactory sessionFactory;

public void save(VoiceMail voicemail) {
    this.sessionFactory.getCurrentSession().save(voicemail);
}

public void update(VoiceMail voicemail) {
    this.sessionFactory.getCurrentSession().update(voicemail);

}

public void delete(VoiceMail voicemail) {
    this.sessionFactory.getCurrentSession().delete(voicemail);

}

}

感谢任何帮助!

谢谢!

1 个答案:

答案 0 :(得分:0)

你能使用注释吗?

它更简单,只需使用Transactional注释你的dao类。

@Transactional

然后所有方法都是自动事务性的,如果你只想要特定的方法,你也可以只为方法自己添加相同的注释。它需要处理事务处理的麻烦。

有关如何实现此目标的详细示例,请参阅this link

----编辑----

此外,如果您对项目有所了解,请查看spring-boot,它将大大加快您的系统开发速度。 Spring - Boot。 该项目有助于为您的系统设置一些最佳实践,让您的工作速度更快。我们目前正在新项目中使用它,它可以非常快速地建立新项目。