Spring MVC-HIbernate Hibernate Version - hibernate-core-5.2.10.Final

时间:2017-10-23 08:19:34

标签: java spring hibernate

Hi To Spring MVC + Hibernate。以前我使用hibernate core-5.1.0.Final比Session.createCriteria()工作正常。但现在它已经贬值了

所以我开始使用hibernate-core-5.2.10.Final。我学会了如何使用session.getCriteriaBuilder()及其工作正常。

但问题是我不知道如何在使用hibernate-core-5.2.10.Final后在数据库中插入更新。以前当我使用hibernate core-5.1.0.Final时,我可以通过使用session.save()轻松地插入更新;调用Session.flush(); session.close();

但是现在我使用session.save();调用Session.flush(); session.close();使用hibernate-core-5.2.10.Final我得到此错误信息: -

  

HTTP状态500 - 请求处理失败;嵌套异常是   javax.persistence.TransactionRequiredException:没有事务在   进展

我的代码是这样的:

public class UsersDAOImpl implements UsersDAO  {

    private static SessionFactory factory ;

    public String registerUser(String userEmailOruserName,String userPassword)
    {



                factory = new Configuration().configure().buildSessionFactory();

                   Session session = factory.openSession();

                         Users us = new Users();
                         us.setUserEmailOruserName(userEmailOruserName);
                         us.setUserPassword(userPassword);

                         try
                         {

                         session.save(us);

                         }

                         catch(HibernateException he)
                         {

                              return "Error";
                         }


                             session.flush();
                             session.close();



                        return "Sucessfull" ;




}

控制台错误消息: -

HTTP Status 500 - Request processing failed; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress


Oct 23, 2017 1:42:10 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/MaharashtraMeinJobDhundho] threw exception [Request processing failed; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress] with root cause
javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3461)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1425)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1421)
    at com.programcreek.MaharashtraMeinJobDhundho.DAO.UsersDAOImpl.registerUser(UsersDAOImpl.java:58)
    at com.programcreek.MaharashtraMeinJobDhundho.Controller.RegisterUserController.registerUser(RegisterUserController.java:31)
    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.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    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 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:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    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)

请告诉我如何使用hibernate-core-5.2.10.Final在DataBase中插入更新。我阅读了很多文章如何做但不能实现它

1 个答案:

答案 0 :(得分:0)

您的例外表示您正在保存/更新没有活动交易的记录。

尝试这样的事情。

try{
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();  
    session.save(us);
    tx.commit(); 
}
catch(){
}