溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
           

          今天在寫struts2 save的時候報了這么一個異常

          java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity

              at org.hibernate.event.SaveOrUpdateEvent.<init>(SaveOrUpdateEvent.java:40)

              at org.hibernate.event.SaveOrUpdateEvent.<init>(SaveOrUpdateEvent.java:23)

              at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)

              at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)

          從上面的異常信息可以看出是在Hibernate進行save的時候檢測到beannull

          1)        大致從這幾個方面來分析原因:

          2)        Hibernate(*.hbm.xml)映射文件是否錯誤

          3)        DAO層代碼是否錯誤

          4)        檢查Action及視圖層jsp頁面等

          從上面幾個逐個檢查后得出的結論為第四條

          首先看Action中的代碼

          private Employee employee;

              public Employee getEmployee() {

                  returnemployee;

              }

              publicvoid setEmployee(Employee employee) {

                  this.employee = employee;

              }

          這里employee是我們觀察的重點

          再看jsp中的代碼

          <s:form action="saveEmployee.action">

                  <s:textfield name="empName" label="empName"></s:textfield>

                  <s:textfield name="age" label="age"></s:textfield>

                  <s:textfield name="sex" label="sex"></s:textfield>

                  <s:textfield name="context" label="context"></s:textfield>

                  <s:submit value="注冊" theme="simple"></s:submit>

                  <s:reset value="取消" theme="simple"></s:reset>

          </s:form>

          大家注意<s:textfield/>name屬性的值

          很顯然錯誤發生在此處修改如下即可:

          <s:form action="saveEmployee.action">

                  <s:textfield name="employee.empName" label="empName"></s:textfield>

                  <s:textfield name="employee.age" label="age"></s:textfield>

                  <s:textfield name="employee.sex" label="sex"></s:textfield>

                  <s:textfield name="employee.context" label="context" value=""></s:textfield>

                  <s:submit value="注冊" theme="simple"></s:submit>

                  <s:reset value="取消" theme="simple"></s:reset>

              </s:form>

          由此我們得出的結論是:

          jsp頁面中一定要保持頁面中各個控件的name屬性與Action中模型驅動對象的名字一致否則就會報錯

          posted on 2009-10-19 11:39 雪山飛鵠 閱讀(27979) 評論(19)  編輯  收藏 所屬分類: Hibernate

          Feedback

          # re: attempt to create saveOrUpdate event with null entity 2010-11-07 16:18 smallmoon
          大哥,你幫了我一個大忙啊,真是,我也是遇到了這個問題。拜讀了你的文章之后,豁然開朗,謝謝哈  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity 2011-02-25 16:10 全球
          我已經這樣寫了 可還是這個錯誤啊  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity 2012-03-15 15:28 someone
          很給力!!!謝謝!!  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity 2012-04-03 22:30 二萬人
          解決了我的問題,謝謝  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity 2012-05-16 16:03 dgd
          怎么回事啊,我有兩個頁面添加,都是用的模型驅動啊,有一個加入了數據,還有一個報這個錯,不能加入數據,怎么有的解決的了,有的解決不了呢。知道底層是怎么實現的嗎。  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity 2012-05-16 16:21 dgd
          這樣還真解決了,謝謝。可是我還是不太清楚。我第一個頁面的時候,也是這樣寫的啊,也是用的模型驅動。是可以把數據加入進去的。第二個頁面加入數據的時候就不行了。用了你上面的方法才解決的。真不知道是為什么?要是可以看底層代碼的話。應該就可以找出是哪里出問題了。  回復  更多評論
            

          # 過客[未登錄] 2012-09-07 16:56 1
          真的很感謝你,問題解決了……  回復  更多評論
            

          # 真給力的工具[未登錄] 2013-03-26 17:28
          真的很給力,特別在spring中使用。。  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity 2013-11-23 16:12 ilex
          因為employee是一個對象@dgd
            回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity[未登錄] 2013-11-23 16:15 candy
          愛死你了!!!  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity 2014-02-20 18:13 sdfwewerw
          我也遇到了,也是忘了加"對象."。  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity[未登錄] 2014-04-03 09:25 w
          有用,非常感謝。  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity 2014-04-27 20:31 多大的
          你這個問題解決了嗎,我跟你一樣啊@全球
            回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity 2014-09-25 10:38 chenbin
          謝謝啦啦啦啦。  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity 2015-01-24 16:40 pei
          一樣的,但是沒解決問題  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity[未登錄] 2015-04-18 20:55 張云
          大哥。你的文章拯救了我啊。我這幾天在做畢業論文,打了一個Spring+hibernate+Strus2框架,可是一測試怎么出現此問題,找了幾天了找不出,郁悶死了。多虧看見你的文章了,總之,謝謝了。呵呵  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity[未登錄] 2016-02-24 10:45 shi
          沒有解決問題  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity[未登錄] 2016-03-08 15:11 影子
          @全球
          <property name="price" type="java.lang.Double">
          <column name="price" not-null="true">
          注意not_null="true",這個地方如果你賦值的時候給了一個空值 也會出這個異常  回復  更多評論
            

          # re: attempt to create saveOrUpdate event with null entity 2016-05-29 18:38 余糖糖
          沒有用,配置文件什么都是好的,寫一個測試類添加數據就可以,但是一到jsp頁面里面就不可以  回復  更多評論
            

          主站蜘蛛池模板: 克什克腾旗| 孝昌县| 山西省| 乌鲁木齐市| 娱乐| 白银市| 彭山县| 依安县| 山西省| 宿松县| 德清县| 田林县| 布拖县| 三门县| 云南省| 星座| 垣曲县| 休宁县| 仲巴县| 清镇市| 贵州省| 蓬安县| 陆川县| 平江县| 扶沟县| 新密市| 乐安县| 灵丘县| 阆中市| 洞口县| 长岭县| 江永县| 绥中县| 古蔺县| 修水县| 岚皋县| 简阳市| 宜君县| 东辽县| 全椒县| 新宁县|