论坛首页 Java企业应用论坛

don't flush the Session after an exception occurs

浏览 3288 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2010-04-30  
更新多条记录出错:Hibernate.
试图在一个Action中调用多次Hibernate插入数据,
因为插入前一条数据后在后一条插入数据中需要用到前一条的记录.
代码如下:
   public ActionForward regUser(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserActionForm uaf = (UserActionForm)form;
        //取出从页面提交过来的数据
        String name = uaf.getName();
        String password = uaf.getPassword();
        String password1 = uaf.getPassword1();
        String nichen = uaf.getNichen();
        String email = uaf.getEmail();
        String province = uaf.getProvince();
        String sex = request.getParameter("sex");
        MD5 md5 = new MD5();
        //将页面提交过来的数据设置进User类中
        User user = new User();
        user.setName(name);
        user.setEmail(email);
        user.setPassword(md5.getMD5ofStr(password));
        user.setNichen(nichen);
        user.setSex(sex);
        user.setProvince(province);
        user.setRegTime(new java.sql.Timestamp(System.currentTimeMillis()));
        //调用业务逻辑方法
        int flag = 0;
        try {
            flag = userManager.add_User(user);
        }catch(Exception e){
            e.printStackTrace();
            return mapping.findForward("add_fail");
        }
        
        /**
         * 默认的方法是开通博客同时是发表一篇开通博客的日志.
         */
        Blog blog = new Blog();
        blog.setName(name);
        blog.setVisited(1);
        blog.setGrade(1);
        blog.setOnline(true);
        blog.setUser(user);
        blog.setLastLoginTime(new java.sql.Timestamp(System.currentTimeMillis()));
        blog.setMingyuan("OpenSource社区博客中心欢迎你");
        blog.setHead(null);
        
        /**
         * 发表一篇开通博客的日志...
         */
        Daily daily = new Daily();
        daily.setTitle("用户"+user.getName()+"开通博客--"+name);
        daily.setContent("<font size='4'><p>感谢用户:<font color='white' size='3'>"+user.getName()+"</font>在OpenSource开源社区中心开通博客</p>" +
                "<p>若有任何问题及意见,请发邮件到*****@163.com或者致电到:*****或者QQ交谈:******</p>" +
                "<p>如果发生问题,我们会尽快帮你解决你所提出的问题,你的意见及问题是我们开源社区前进的动力!</p>"+
                "<p>谢谢你的合作!</p></font><p align='right'>开发团队PPP敬上!</p>");
        daily.setVisited(0);
        daily.setPostTime(new java.sql.Timestamp(System.currentTimeMillis()));
        int flag2 = 0;
        try {
            flag2 = blogManager.add_Blog(blog);
        }catch(Exception e){
            e.printStackTrace();
            return mapping.findForward("add_fail");
        }
        daily.setBlog(blog);
        try {
            dailyManager.add_Daily(daily);
        }catch(Exception e){
            e.printStackTrace();
            return mapping.findForward("add_fail");
        }
        if(flag==1 && flag2==1){
            return mapping.findForward("add_success");
        }else {
            return mapping.findForward("add_fail");
        }
    }
    



javax.servlet.ServletException: org.hibernate.AssertionFailure: null id in com.hpjianhua.xiehui.model.User entry (don't flush the Session after an exception occurs)
    org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:78)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)


root cause 

org.hibernate.AssertionFailure: null id in com.hpjianhua.xiehui.model.User entry (don't flush the Session after an exception occurs)
    org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:55)
    org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:157)
    org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113)
    org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
    org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
    org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
    org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:558)
    org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:540)
    org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:510)
    org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:310)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:209)
    $Proxy24.add_User(Unknown Source)
    com.hpjianhua.xiehui.web.action.UserAction.regUser(UserAction.java:52)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
    org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
    com.hpjianhua.xiehui.web.action.BaseAction.execute(BaseAction.java:18)
    org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:78)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.


   发表时间:2010-04-30  
请问解决方法....谢谢了..
0 请登录后投票
   发表时间:2010-04-30  
>>因为插入前一条数据后在后一条插入数据中需要用到前一条的记录
先去了解一下hibernate的基础知识吧

-----------------------------------
Action的代码,简直惨不忍睹
0 请登录后投票
   发表时间:2010-04-30  
看看兄弟写的Action代码是如何的呢?谢谢了
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics