天天看點

Hibernate常見錯誤整理

常見錯誤:

1.Caused by: org.dom4j.DocumentException: Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.

如果出現這行錯誤說明你的XML配置檔案有不規範的字元,檢查下。

2.net.sf.hibernate.MappingException: Error reading resource: hibernate/Hello_Bean.hbm.xml

如果出現這行錯誤說明你的hibernate的XML配置檔案有錯

3.net.sf.hibernate.MappingException: Resource: hibernate/Hello_Bean.hbm.xml not found

如果出現這行錯誤說明hibernate的XML配置檔案沒有找到,你應該把XML檔案放在與你的類檔案同個目錄下,一般跟同名的java持久化類放在一起,也就是跟Hello_Bean.class類檔案一起。

4.net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class hibernate.Hello_Bean

如果出現這行錯誤說明你的xml檔案裡設定的字段名name的值與Hello_Bean.Java類裡的getXXX或setXXX方法不一緻。

5.net.sf.hibernate.HibernateException: JDBC Driver class not found: org.gjt.mm.mysql.Driver:沒有找到資料庫驅動程式

6.The database return no natively generate indentity value。主鍵沒有添加增量

7.net.sf.hibernate.PropertyValueException:not-null property references a null or transient value:com.pack.Rordercontent.異常産生原因:Rordercontent對象的非空屬性Rordertable引用了一個臨時對象。

8.net.sf.hibernate.TransientobjectException:objiect references an unsaved transient instance – save the transient instance before flushing: com.pack.Rordertable

持久化對象的某個屬性引用了一個臨時對象Rordertable

9.net.sf.hibernate.MappingException 當出現net.sf.hibernate.MappingException: Error reading resource:…異常時一般是因為映射檔案出現錯誤。 當出現net.sf.hibernate.MappingException: Resource: … not found是因為XML配置檔案沒找到所緻,有可能是放置目錄不正确,或者沒将其加入hibernate.cfg.xml中。

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

11. 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的取值可以為increment、identity、sequence、hilo、native……等,更多的可參考hibernate參考文檔,一般取其值為native 。具體可參考2.2.2.1小節。

12. 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)因為該方法是從hibernate的session中去重新取object,如果session中沒有這個對象,則會報錯是以當你使用saveOrUpdate(object)之前還需要判斷一下。 (3)session.merge(object),Hibernate裡面自帶的方法,推薦使用。

13. SQL Grammer Exception,Could not execute JDBC batch update 當出現SQL Grammer Exception,Could not execute JDBC batch update異常時,一般是由如下問題引起: (1)SQL語句中存在文法錯誤或是傳入的資料有誤; (2)資料庫的配置不合法,或者說是配置有誤。較容易出現的有資料表的映射檔案(,hbm.xml檔案)配置有誤;Hibernate.cfg.xml檔案配置有誤; (3) 目前的資料庫使用者權限不足,不能操作資料庫。以是以Oracle 資料庫為例,這種情況下在錯誤提示中會顯示java.sql.BatchUpdateException: orA-01031: insufficient privileges這樣的資訊。

14.net.sf.hibernate.HibernateException:identifier of an instance of my chapter.pack6.Rordertable altered from 1 to 100: 企圖修改處于持久化狀态的對象的OID。修改了處于持久化對象的OID在Session清理緩存時就會抛出此異常(對象處于持久化狀态時。不允許程式随意修改它的OID。注意:無論java對象處于臨時狀态、持久化狀态還是遊離狀态,應用程式都不應該修改它的OID。因為,比較安全的做法時,在定義持久化類時,吧他的setId()方法設為private類型,禁止外部程式通路該方法)。

15.net.sf.hibernate.MappingException: Unknown entity class:未知的實體類

    Hibernate把持久化類的屬性分為2種:值(Value)類型和實體(Entity)類型。值類型和實體類型最重要的差別是前者沒有OID,不能被單獨持久化,它的聲明周期依賴于所屬的持久化類的對象的聲明周期,元件類型就是一種值類型;而實體類型有OID,可以被單獨持久化。

16.net.sf.hibernate.QueryException: undefined alias:我猜想出項這種錯誤的原因有很多種:可能是大小寫問題,還有其他很多種可能

17.net.sf.hibernate.NonUniqueResultException:檢索單個對象時,查詢結果包含多個對象,但沒有調用setMaxResult(1)方法

18.net.sf.hibernate.QueryException: Not all named parameters have been set

使用setProperties()方法:用于把命名參數與一個對象的屬性值綁定時,對象中沒有比對的名字相同的屬性。

繼續閱讀