太陽雨

          痛并快樂著

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            67 Posts :: 3 Stories :: 33 Comments :: 0 Trackbacks
          本文總結Hibernate中常見的異常。

          1. net.sf.hibernate.MappingException

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

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

          2. net.sf.hibernate.PropertyNotFoundException

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

          3. org.hibernate.id.IdentifierGenerationException

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

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

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

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

                 有如下幾種解決方案:

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

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

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

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

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

          1SQL語句中存在語法錯誤或是傳入的數(shù)據(jù)有誤;

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

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

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

          posted on 2008-10-21 14:29 小蟲旺福 閱讀(9255) 評論(2)  編輯  收藏 所屬分類: hibernate相關

          Feedback

          # re: Hibernate常見異常總結(不斷補充) 2015-08-04 15:42 re: Hibernate常見異常總結(不斷補充)
          re: Hibernate常見異常總結(不斷補充) re: Hibernate常見異常總結(不斷補充) re: Hibernate常見異常總結(不斷補充) re: Hibernate常見異常總結(不斷補充) re: Hibernate常見異常總結(不斷補充) re: Hibernate常見異常總結(不斷補充) re: Hibernate常見異常總結(不斷補充) re: Hibernate常見異常總結(不斷補充) re: Hibernate常見異常總結(不斷補充) re: Hibernate常見異常總結(不斷補充) re: Hibernate常見異常總結(不斷補充) re: Hibernate常見異常總結(不斷補充)   回復  更多評論
            

          # re: Hibernate常見異常總結(不斷補充) 2015-08-04 15:43 re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
          @re: Hibernate常見異常總結(不斷補充)
            回復  更多評論
            

          主站蜘蛛池模板: 师宗县| 平遥县| 合山市| 苏尼特左旗| 长沙市| 光泽县| 兰州市| 锡林郭勒盟| 陆丰市| 黔东| 抚顺市| 农安县| 即墨市| 阿拉善盟| 塔河县| 嫩江县| 六安市| 望江县| 廊坊市| 精河县| 营山县| 阜南县| 新竹市| 修文县| 青川县| 兴城市| 广水市| 嫩江县| 古丈县| 尼勒克县| 清镇市| 灵武市| 庆元县| 甘孜县| 二手房| 杨浦区| 乾安县| 静安区| 漠河县| 吉林省| 长治县|