class LegacyCdDomain {
String title
Thumbnail thumbnail
static mapping = {
thumbnail ignoreNotFound: true
}
}
ignoreNotFound
目的
指定在多對一關係中如何處理參照不存在列的外來鍵。
範例
class Thumbnail {
byte[] data
}
說明
用法:association_name(ignoreNotFound: boolean)
如果資料庫中的資料損毀,而外來鍵參照不存在的記錄,Hibernate 會顯示「No row with the given identifier exists」訊息並抱怨。例如,LegacyCdDomain
記錄可能參照 ID 為 1234 的 Thumbnail
記錄,但資料庫中可能不再包含 ID 為 1234 的 Thumbnail
。
載入此類損毀資料的一種可能方式是使用 ignoreNotFound
對應選項。如果您將其設定為 true
,Hibernate 會將不存在的列視為 null
關聯。因此,在上述範例中,我們的 LegacyCdDomain
執行個體會載入,但其 thumbnail
屬性會為 null
。為 ignoreNotFound
指定 false
值會導致 Hibernate 擲出 org.hibernate.ObjectNotFoundException
。
載入資料集時,如果 ignoreNotFound: true ,可能會因為遺失參考而擲出例外狀況!
|
這個設定的預設值為 false
,對應到 Hibernate 的 not-found
屬性。
如果你有一個舊有的資料庫,在參考完整性方面有異常行為,這個設定會很有用。