【Hibernate總結(jié)系列】常見異常總結(jié)(不斷補(bǔ)充)

           

                 本文總結(jié)Hibernate中常見的異常。

          1. net.sf.hibernate.MappingException

                 當(dāng)出現(xiàn)net.sf.hibernate.MappingException: Error reading resource:…異常時(shí)一般是因?yàn)橛成湮募霈F(xiàn)錯(cuò)誤。

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

          2. net.sf.hibernate.PropertyNotFoundException

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

          3. org.hibernate.id.IdentifierGenerationException

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

                 解決方案:<id>元素映射了相應(yīng)數(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

                 當(dāng)出現(xiàn)a different object with the same identifier value was already associated with the session時(shí),一般是因?yàn)樵?/span>hibernate中同一個(gè)session里面有了兩個(gè)相同標(biāo)識但是是不同實(shí)體。

                 有如下幾種解決方案:

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

          2)使用session.refresh(object),當(dāng)object不是數(shù)據(jù)庫中已有數(shù)據(jù)的對象的時(shí)候,不能使用session.refresh(object)因?yàn)樵摲椒ㄊ菑?/span>hibernatesession中去重新取object,如果session中沒有這個(gè)對象,則會報(bào)錯(cuò)所以當(dāng)你使用saveOrUpdate(object)之前還需要判斷一下。

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

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

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

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

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

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

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

          http://www.aygfsteel.com/amigoxie/category/19976.html

          posted on 2008-11-15 21:52 smallfa 閱讀(289) 評論(0)  編輯  收藏 所屬分類: hibernate/ibatis

          <2008年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          smallfa
          博客園
          C++博客
          博客生活
          Blogjava
          足球博客
          微博
          Redsaga

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          Ajax

          Blogs

          DB

          java

          Open source

          ORM

          Tools/Help

          vedio Tech

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 麻栗坡县| 天长市| 新绛县| 始兴县| 南城县| 延吉市| 易门县| 垦利县| 祁连县| 上犹县| 新密市| 凤庆县| 盘锦市| 大庆市| 香河县| 嘉善县| 东乌珠穆沁旗| 龙口市| 清水河县| 达拉特旗| 寿宁县| 荃湾区| 囊谦县| 大冶市| 旌德县| 文安县| 铜梁县| 介休市| 绵竹市| 浙江省| 新津县| 鄂伦春自治旗| 平山县| 六安市| 岗巴县| 右玉县| 肇东市| 建湖县| 衡阳市| 鹤壁市| 社旗县|