1. 程式人生 > >No row with the given identifier exists 解決方法(集錦)

No row with the given identifier exists 解決方法(集錦)

No row with the given identifier exists 解決方法

出現異常org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: No row with the given identifier exists

一.

No row with the given identifier exists 解決方法
有兩張表,a和b.產生此問題的原因就是a裡做了關聯<one- to-one>或者<many-to-one unique="true">(特殊的多對一對映,實際就是一對一)來關聯b.當hibernate查詢的時候,b裡的資料沒有與a相匹配的,這樣就會報No row with the given identifier exists這個錯.(一句話,就是資料的問題!)

      假如說,a裡有自身的主鍵id1,還有b的主鍵id2,這兩個欄位.

      如果hibenrate設定的單項關聯,即使a中的id2為null值,b中id2中有值,查詢都不會出錯.但是如果a中的id2欄位有值,但是這個值在b中主鍵值裡並沒有,就會報上面的錯!

      如果hibernate是雙向關聯,那麼a中的id2為null值,但是b中如果有值,就會報這個錯.這種情況目前的解決辦法就是改成單項關聯,或者把不對應的資料改對!


二.
hibernate的一些常見的錯誤

No row with the given identifier exists

表示你現在查詢的物件所關聯的物件有問題,一般是因為資料的問題(該物件所關聯的物件找不到)

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配置檔案有不規範的字元,檢查下。

net.sf.hibernate.MappingException: Error reading resource: hibernate/Hello_Bean.hbm.xml
如果出現這行錯誤說明你的hibernate的XML配置檔案有錯

net.sf.hibernate.MappingException: Resource: hibernate/Hello_Bean.hbm.xml not found
如果出現這行錯誤說明hibernate的XML配置檔案沒有找到,你應該把XML檔案放在與你的類檔案同個目錄下,本文中是放在hibernateclasseshibernate目錄下,也就是跟Hello_Bean.class類檔案一起。

net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class hibernate.Hello_Bean
如果出現這行錯誤說明你的xml檔案裡設定的欄位名name的值與Hello_Bean.java類裡的getXXX或setXXX方法不一致。

net.sf.hibernate.HibernateException: JDBC Driver class not found: org.gjt.mm.mysql.Driver
如果出現這行錯誤說明你的MYSQL驅動沒有加進JB庫裡或者不在CLASSPATH裡。

三.
這個異常是在 多對一關係對映時,一方表中對應的資料不存在才丟擲的。
原來的配置:
<many-to-one class="com.art.model.user.UserInfo" fetch="join" name="userInfo" >
   <column name="userId" unique="true"/>
</many-to-one>

修改後的:
<many-to-one class="com.art.model.user.UserInfo" fetch="join" name="userInfo" not-found="ignore">
   <column name="userId" unique="true"/>
</many-to-one>

紅色是修改的部分。意思是當對應的資料不存在時 忽略掉,用null值填充。該屬性預設值:exception 。

四.
今天整理許可權和選單關聯的時候,報出了No row with the given identifier exists,查了半天中間表,找了一些資料,轉過來記載一下


產生此問題的原因:

         有兩張表,table1如使用者表user,和table2角色表role.產生此問題的原因就是table1裡做了關聯<one-to-one>或者<many-to-one unique="true">(特殊的多對一對映,實際就是一對一)來關聯table2.

     當hibernate查詢的時候,table2角色表role裡的資料沒有與table1使用者表user相匹配時,這樣就會報No row with the given identifier exists這個錯.(一句話,就是資料的問題!)

       假如說,table1使用者表user裡有自身的主鍵id1,還有table2角色表role的主鍵id2(其實是table2角色表role的外來鍵),這兩個欄位.
         如果hibenrate設定的單項關聯,即使table1中的id2為null值,table2中id2中有值,查詢都不會出錯.但是如果table1中的id2欄位有值,但是這個值在table2中主鍵值裡並沒有,就會報上面的錯!

         如果hibernate是雙向關聯,那麼table1中的id2為null值,但是table2中如果有值,就會報這個錯.這種情況目前的解決辦法就是改成單項關聯,或者把不對應的資料改對! (我就是這樣改好的)

           這就是報這個錯的原因了,知道原因了就相應的改就行了.或許還有些人迷惑hibernate關聯都配好了,怎麼會出現這樣的錯?其實這是程式設計的時候出現的問題,假如說我在新增資訊的時候,頁面傳過來的struts的formbean到dao方法中需要封裝成hibernate的po(就是hibenrate的bean),要是一個個po.get(form.set())實在太麻煩了,這樣一般都會寫個專門的方法來封裝,遇到po.get(form.set())這種情況直接把struts的formbean物件傳到此方法中封裝就行了,假如我有個欄位是建立人id,那麼這個欄位是永遠不會改的,我在新增的時候還呼叫這個方法,這個專門封裝的方法是有一些判斷的,假如說我判斷一下,如果遇到建立人id傳過來為空值,我判斷如果是空值,我把建立人id設為0,但是使用者表中userid是主鍵從1開始自增的,那麼這樣資料就對應不上了,一查就會出這個錯了.這個錯在開發剛開始的時候經常發生,因為每個人的模組都是由相應的人獨立開發完成以後再整合在一起的,每個人寫單獨那一塊的時候往往會忽略這些,所以整合的時候這些問題往往就都一下子全冒出來了....整合很辛苦,tnnd!

hibernate的查詢的比較
hibernate的查詢有很多,Query,find,Criteria,get,load

query使用hsql語句,可以設定引數是常用的一種方式

criteria的方式,儘量避免了寫hql語句,看起來更面向物件了。

find方式,這種方式已經被新的hibernate丟棄

get和load方式是根據id取得一個記錄
下邊詳細說一下get和load的不同,因為有些時候為了對比也會把find加進來。

1,從返回結果上對比:
load方式檢索不到的話會丟擲org.hibernate.ObjectNotFoundException異常
get方法檢索不到的話會返回null

2,從檢索執行機制上對比:
get方法和find方法都是直接從資料庫中檢索
而load方法的執行則比較複雜
1,首先查詢session的persistent Context中是否有快取,如果有則直接返回
2,如果沒有則判斷是否是lazy,如果不是直接訪問資料庫檢索,查到記錄返回,查不到丟擲異常
3,如果是lazy則需要建立代理物件,物件的initialized屬性為false,target屬性為null
4, 在訪問獲得的代理物件的屬性時,檢索資料庫,如果找到記錄則把該記錄的物件複製到代理物件的target
上,並將initialized=true,如果找不到就丟擲異常

Hibernate: No row with the given identifier exists
鬱悶了好久,終於在goole上找到了錯誤原因
ObjectNotFoundException: No row with the given identifier exists
Where it can occur:

This might occur if you try to load a non-proxied object with session.load() or you load a proxied object and later access the proxy. It may also occur when loading mapped collections which are not eagerly fetched.
What it means:

This means just what it says - Hibernate expected to have a row with a certain id in the database, and did however not find it.
How it can be caused:
Wrong id for load

If you are using a nonexistant id for a load(), you will get this exception. Note that if you are using proxies, this may occur later on, when the proxy is first "broken".
Broken foreign key relationships

If you map for example a many-to-one relationship, and the table containing the objects on the "many" side contains a foreign key to a non-existant row of the "one" side table, you will get such an exception. This will only happen if the collection is not fetched eagerly.
Reusing a session across multiple transactions

The hibernate session internally memorizes some state. For example it internally stores ids which it assumes are already deleted from the database. So if you make the session believe somehow an object with a certain id has been deleted, and later try to load it again, you will get this exception without Hibernate even trying to access the database. So be careful with that.
How it can be fixed:

    Check the ids you are using for load
    Validate your foreign key relationships - the database should already maintain such integrity constraints.
    Beware of multiple transactions within a single session. Try to stick to "one session, one transaction" rule. Especially do not reuse sessions after an HibernateException.

也就是是說關係資料庫一致性遭到了破壞,找到相關表,就可以解決了