今天寫了個程序 Spring整合Hibernate 使用Spring中的HibernateTemplate 通過配置程序可以運行起來 并可以操作數(shù)據(jù)庫 但程序執(zhí)行過程中會有異常 不知道是什么錯誤 一下是程序執(zhí)行的異常
          Hibernate: insert into test.student (STUNAME, AGE, SEX, PHONE) values (????)
          java.lang.RuntimeException
              at com.stu.server.StudentServer.addStudentByCondition(StudentServer.java:
          30)
              at com.stu.server.StudentServer$$FastClassByCGLIB$$8fef4d1a.invoke(
          <generated>)
              at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:
          149)
              at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:
          696)
              at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:
          149)
              at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:
          106)
              at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:
          171)
              at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:
          631)
              at com.stu.server.StudentServer$$EnhancerByCGLIB$$f5f4d830.addStudentByCondition(
          <generated>)
              at com.stu.test.Test.main(Test.java:
          33)
          Hibernate: update test.student set STUNAME
          =?, AGE=?, SEX=?, PHONE=? where ID=?
          還麻煩各位幫忙指點一下 程序我上傳上來了,另外還請哪個大哥幫忙指出這程序的不足之處

          程序下載地址:http://www.aygfsteel.com/Files/lifenote/HibernateInSpring.rar
          posted on 2007-12-07 10:20 LifeNote 閱讀(1799) 評論(11)  編輯  收藏 所屬分類: JavaHibernateSpring
          Comments
          • # re: Spring整合Hibernate 莫名其妙的錯誤
            LifeNote
            Posted @ 2007-12-07 11:12
            我知道異常了 我使用的是spring的聲明事務(wù) 當(dāng)年齡小于30的時候就拋異常并且回滾事務(wù) 可現(xiàn)在并沒有回滾 還麻煩個位看看 多謝
            getStudentDAO().save(stu);
            stu.setAge( stu.getAge()+10);
            if(stu.getAge()<30){ //這里拋個異常就回滾了 不用rollback了
            throw new RuntimeException();
            }  回復(fù)  更多評論   
          • # re: Spring整合Hibernate 莫名其妙的錯誤 [未登錄]
            flustar
            Posted @ 2007-12-07 11:28
            建議你把hbm文件中的 catalog="test" 去掉 另外你的hibernate.cfg.xml是多余的  回復(fù)  更多評論   
          • # re: Spring整合Hibernate 莫名其妙的錯誤
            Tiger F
            Posted @ 2007-12-07 11:32
            兩個問題:
            既然使用了spring的聲明事物,就不應(yīng)該再自己聲明事物了。spring的聲明事物就是檢查是否有異常出現(xiàn)來決定最后是提交還是回滾的,所以必須使用異常。
            所以你的server程序應(yīng)該寫為:(似乎根本不需要回滾)
            public void addStudentByCondition(Student stu){
            if(stu.getAge()>=30)
            getStudentDAO().save(stu);
            }
            如果因為有其他邏輯需要回滾的話,應(yīng)該這樣:
            public void addStudentByCondition(Student stu){
            if(stu.getAge()<30)
            throw new MyBizException(); // 建議自定義一個有業(yè)務(wù)意義的異常
            getStudentDAO().save(stu);
            }

            另一個問題,你的Dao類在使用templage方法是都自己做了異常處理,但是你的異常處理無實質(zhì)內(nèi)容,這個做法會影響到事物的。如果你不知道應(yīng)該如何處理這些異常,最好就是不要管他。
              回復(fù)  更多評論   
          • # re: Spring整合Hibernate 莫名其妙的錯誤
            LifeNote
            Posted @ 2007-12-07 11:34
            catalog="test" 這個只是數(shù)據(jù)庫的名字啊  回復(fù)  更多評論   
          • # re: Spring整合Hibernate 莫名其妙的錯誤
            LifeNote
            Posted @ 2007-12-07 11:38
            既然我在
            if(stu.getAge()<30){ //這里拋個異常就回滾了 不用rollback了
            throw new RuntimeException();
            }這里拋了異常為什么程序還是沒有回滾呢  回復(fù)  更多評論   
          • # re: Spring整合Hibernate 莫名其妙的錯誤
            Tiger F
            Posted @ 2007-12-07 14:41
            如果你的程序還是原來那個樣子,雖然拋出了異常也是被你自己捕獲了,并沒有被spring捕獲到。
            spring既然不知道發(fā)生了異常,當(dāng)然不會回滾事物了。
              回復(fù)  更多評論   
          • # re: Spring整合Hibernate 莫名其妙的錯誤
            LifeNote
            Posted @ 2007-12-07 16:28
            請問那要如何處理事務(wù)呢 我有些糊涂了 麻煩 把那程序修改后把代碼貼上來 好么 十分感謝 學(xué)習(xí)中  回復(fù)  更多評論   
          • # re: Spring整合Hibernate 莫名其妙的錯誤
            Tiger F
            Posted @ 2007-12-07 16:30
            我貼了呀,上面。只要把addStudentByCondition函數(shù)的實現(xiàn)完整替換一下,至少是能回滾了。  回復(fù)  更多評論   
          • # re: Spring整合Hibernate 莫名其妙的錯誤
            LifeNote
            Posted @ 2007-12-07 16:35
            您是說
            if(stu.getAge()<30)
            throw new MyBizException(); // 建議自定義一個有業(yè)務(wù)意義的異常
            getStudentDAO().save(stu);
            }

            這里new MyBizException(); 是自己捕獲了么 ?
            那這樣和你說的 這個:

            雖然拋出了異常也是被你自己捕獲了,并沒有被spring捕獲到。
            spring既然不知道發(fā)生了異常,當(dāng)然不會回滾事物了。
            也沒有被spring知道啊 如何回滾呢
              回復(fù)  更多評論   
          • # re: Spring整合Hibernate 莫名其妙的錯誤
            Tiger F
            Posted @ 2007-12-07 16:59
            public void addStudentByCondition(Student stu){
            if(stu.getAge()<30)
            throw new MyBizException(); // 可以用RuntimeException替換
            getStudentDAO().save(stu);
            }

            注意,整個addStudentByCondition函數(shù)應(yīng)該就這幾行,而不是修改這三行。
            把你的try/catch之類的統(tǒng)統(tǒng)刪除。  回復(fù)  更多評論   
          • # re: Spring整合Hibernate 莫名其妙的錯誤
            LifeNote
            Posted @ 2007-12-07 17:02
            哦 明白你的意思了 十分感謝!!!  回復(fù)  更多評論   
           
          主站蜘蛛池模板: 农安县| 虞城县| 宜黄县| 赞皇县| 和硕县| 锦州市| 修水县| 沭阳县| 阜新| 章丘市| 玛纳斯县| 溆浦县| 塔城市| 普兰店市| 临城县| 昭平县| 喀喇沁旗| 江阴市| 潮州市| 手游| 汉沽区| 洱源县| 象州县| 汉中市| 射洪县| 内黄县| 昌邑市| 扎兰屯市| 徐州市| 阳东县| 霞浦县| 澄迈县| 五莲县| 太保市| 监利县| 肃北| 平山县| 调兵山市| 新宾| 原平市| 淮阳县|