`
hpjianhua
  • 浏览: 237144 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类

don't flush the Session after an exception occurs

 
阅读更多
更新多条记录出错: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.


分享到:
评论
3 楼 hpjianhua 2010-04-30  
看看兄弟写的Action代码是如何的呢?谢谢了
2 楼 rainlife 2010-04-30  
>>因为插入前一条数据后在后一条插入数据中需要用到前一条的记录
先去了解一下hibernate的基础知识吧

-----------------------------------
Action的代码,简直惨不忍睹
1 楼 hpjianhua 2010-04-30  
请问解决方法....谢谢了..

相关推荐

    hibernate的session.flush

    博文链接:https://llying.iteye.com/blog/221702

    hibernate的flush()、refresh()、clear()针对一级缓存的操作的区别.docx

    session.flush()和session.clear()就针对session的一级缓存的处理。 简单的说, 1 session.flush()的作用就是将session的缓存中的数据与数据库同步。 2 session.clear()的作用就是清除session中的缓存数据(不管缓存...

    BobBuilder_app

    Embedded: You can use RaptorDB inside your application as you would any other DLL, and you don't need to install services or run external programs. NoSQL: A grass roots movement to replace relational...

    通过HttpURLConnection获取SESSIONID

    out.flush(); out.close(); out.write(parameters.getBytes(),0,parameters.getBytes().length); InputStream in = hc.getInputStream(); InputStreamReader reader = new InputStreamReader(in,"gb...

    微软内部资料-SQL性能优化2

    By leveraging the Address Windowing Extensions API, an application can create a fixed-size window into the additional physical memory. This allows a process to access any portion of the physical ...

    Hibernate的事务处理机制和flush方法的用法.docx

    an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session) net.sf.hibernate.AssertionFailure: possible nonthreadsafe access to session ...

    mmu.rar_flush

    clflush should apparently flush the cache line on all processors in an SMP system.

    三星flush接口定义

    三星flush接口定义图解三星flush接口定义图解

    proc-xscale.rar_flush

    This is the maximum size of an area which will be flushed. If the area is larger than this, then we flush the whole cache.

    数位板压力测试

    This specification is intended to be an open standard, and as such the text and information contained herein may be freely used, copied, or distributed without compensation or licensing restrictions. ...

    servlet2.4doc

    Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the chain, causes the resource at the end of the chain to be invoked. doFilter(ServletRequest, ...

    php.ini-development

    An empty string can be denoted by simply not writing anything after the equal ; sign, or by using the None keyword: ; foo = ; sets foo to an empty string ; foo = None ; sets foo to an empty string ;...

    module_pg_flushbuffer

    module_pg_flushbuffer

    hibernate的flush机制

    对hibernate的flush机制有兴趣可以看看

    flush插件下载

    flush插件下载

    深入理解ob_flush和flush的区别(ob_flush()与flush()使用方法)

    注意:ob_flush()和flush()这两个函数一般要一起使用,顺序是先ob_flush(),然后flush(),它们的作用是刷新缓冲区。这里具体的说下什么时候要用到刷新缓冲区和为什么要刷新缓冲区。 一、什么时候要刷新缓冲区 当...

    Hibernate的Session_flush与隔离级别代码详解

    主要介绍了Hibernate的Session_flush与隔离级别代码详解,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下

    MySQL-Flush命令用法.docx#资源达人分享计划#

    MySQL-Flush命令用法.docx#资源达人分享计划#

    eac3to V3.17

    * added a flush after every log line to help GUIs * "eac3to some.mpls" now also works if the stream files aren't there, anymore * fixed: number of subtitles was not appended to demuxed subtitles' file...

    dump_psb.rar_The Just

    Some ColdFire parts implement separate instruction and data caches, on those we should just flush the appropriate cache. If we don t need to do any specific flushing then this will be optimized away.

Global site tag (gtag.js) - Google Analytics