posts - 297,  comments - 1618,  trackbacks - 0
           

                 本文總結Hibernate中常見的異常。

          1. net.sf.hibernate.MappingException

                 當出現net.sf.hibernate.MappingException: Error reading resource:…異常時一般是因為映射文件出現錯誤。

                 當出現net.sf.hibernate.MappingException: Resource: … not found是因為XML配置文件沒找到所致,有可能是放置目錄不正確,或者沒將其加入hibernate.cfg.xml中。

          2. net.sf.hibernate.PropertyNotFoundException

                 當出現net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class …時,原因一般是因為XML映射文件中的屬性與對應的Java類中的屬性的gettersetter方法不一致。

          3. org.hibernate.id.IdentifierGenerationException

                 當出現org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():異常時,一般是因為<id>元素配置不正確,<id>元素缺少其子元素<generator></generator>的配置引起。

                 解決方案:<id>元素映射了相應數據庫表的主鍵字段,對其子元素<generator class="">,其中class的取值可以為incrementidentitysequencehilonative……等,更多的可參考hibernate參考文檔,一般取其值為native 。具體可參考2.2.2.1小節。

          4. a different object with the same identifier value was already associated with the session

                 當出現a different object with the same identifier value was already associated with the session時,一般是因為在hibernate中同一個session里面有了兩個相同標識但是是不同實體。

                 有如下幾種解決方案:

          1)使用session.clean(),如果在clean操作后面又進行了saveOrUpdate(object)等改變數據狀態的操作,有可能會報出"Found two representations of same collection"異常。

          2)使用session.refresh(object),當object不是數據庫中已有數據的對象的時候,不能使用session.refresh(object)因為該方法是從hibernatesession中去重新取object,如果session中沒有這個對象,則會報錯所以當你使用saveOrUpdate(object)之前還需要判斷一下。

          3session.merge(object)Hibernate里面自帶的方法,推薦使用。

          5. SQL Grammer Exception,Could not execute JDBC batch update

                 當出現SQL Grammer Exception,Could not execute JDBC batch update異常時,一般是由如下問題引起:

          1SQL語句中存在語法錯誤或是傳入的數據有誤;

          2)數據庫的配置不合法,或者說是配置有誤。較容易出現的有數據表的映射文件(,hbm.xml文件)配置有誤;Hibernate.cfg.xml文件配置有誤;

          3 當前的數據庫用戶權限不足,不能操作數據庫。以是以Oracle 數據庫為例,這種情況下在錯誤提示中會顯示java.sql.BatchUpdateException: ORA-01031: insufficient privileges這樣的信息。

                 針對上面的各種原因,開發人員可以找出對應的解決方案。

          posted on 2008-01-07 14:43 阿蜜果 閱讀(12508) 評論(9)  編輯  收藏 所屬分類: Hibernate


          FeedBack:
          # re: 【Hibernate總結系列】常見異常總結(不斷補充)
          2008-01-08 11:01 | hanson
          感覺你的blog分類很多,涉及面很廣啊。。。也許可以更專注點吧  回復  更多評論
            
          # re: 【Hibernate總結系列】常見異常總結(不斷補充)
          2008-01-08 11:08 | 阿蜜果
          @hanson
          多謝指點!
          最近總結的都是一些工作上在用的東西,權當備忘的,呵呵
          有了往某個方面專注的想法的。嘿嘿  回復  更多評論
            
          # 寫點高級的
          2008-01-12 18:16 | 456
          太菜了,這些都是低級錯誤,做開發的要犯,自殺算了,  回復  更多評論
            
          # re: 【Hibernate總結系列】常見異常總結(不斷補充)
          2008-01-17 16:50 | 99
          ddd  回復  更多評論
            
          # re: 【Hibernate總結系列】常見異常總結(不斷補充)
          2008-01-17 16:56 | 99
          我遇到以下問題,請問怎么解決:
          net.sf.hibernate.MappingException: Dialect does not support identity key generation


          **
          1,使用的是SQL 2000數據庫
          2,已經在數據庫表里設置了主鍵,自動增長
          **  回復  更多評論
            
          # re: 【Hibernate總結系列】常見異常總結(不斷補充)
          2008-04-23 21:15 | 銀河使者
          不是說了嗎Dialect不支持identity key generation ,你是用的increment吧,如果在數據庫中設主鍵,就不用hibernate的key產生器了  回復  更多評論
            
          # re: 【Hibernate總結系列】常見異常總結(不斷補充)
          2010-01-12 11:38 | 564
          @456
            回復  更多評論
            
          # re: 【Hibernate總結系列】常見異常總結(不斷補充)
          2010-01-12 11:40 | 86
          你的想法我不反對,但你也要考慮到初學者呀!  回復  更多評論
            
          # re: 【Hibernate總結系列】常見異常總結(不斷補充)
          2010-03-25 13:07 | peterman
          確實涉獵很廣  回復  更多評論
            
          <2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

                生活將我們磨圓,是為了讓我們滾得更遠——“圓”來如此。
                我的作品:
                玩轉Axure RP  (2015年12月出版)
                

                Power Designer系統分析與建模實戰  (2015年7月出版)
                
               Struts2+Hibernate3+Spring2   (2010年5月出版)
               

          留言簿(263)

          隨筆分類

          隨筆檔案

          文章分類

          相冊

          關注blog

          積分與排名

          • 積分 - 2298133
          • 排名 - 3

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 翁源县| 牡丹江市| 咸丰县| 嫩江县| 穆棱市| 嘉禾县| 开江县| 电白县| 吐鲁番市| 荆州市| 墨玉县| 鄄城县| 南雄市| 八宿县| 乌恰县| 凤山市| 博野县| 当涂县| 鄱阳县| 上饶县| 武义县| 武城县| 泰和县| 介休市| 岢岚县| 华阴市| 新乡县| 徐水县| 定安县| 荥阳市| 辛集市| 黄陵县| 建湖县| 深水埗区| 嘉峪关市| 岑溪市| 崇礼县| 图们市| 平邑县| 合江县| 泸州市|