2009-07-16 48 views
1

我有以下映射:NHibernate的集合映射引發「無法加載或初始化對象或集合」

<bag name="BList" table="A_TABLE" inverse="true" lazy="false" cascade="all-delete-orphan"> 
    <key column="A_ID"/> 
    <one-to-many 
    class="B, Model" /> 
</bag> 

而且

<many-to-one name="A" 
      class="A, Model" 
      column="A_ID" 
      not-null="true" /> 

執行插入和更新工作正常(加時和從集合中移除),但是在拋出異常時總是獲取對象結果。

failed: NHibernate.Exceptions.GenericADOException : could not load an entity: [Model.B#5816932][SQL: SELECT ...] 
    ----> System.NullReferenceException : Object reference not set to an instance of an object. 

或者......

NHibernate.Exceptions.GenericADOException : could not initialize a collection: [Model.A.BList#1364389][SQL: ...] 
    ----> System.NullReferenceException : Object reference not set to an instance of an object. 

...這取決於什麼對象,你要提取上。 我確定我在這裏錯過了一件簡單的事情,因爲它在我實施cascade =「all-delete-orphan」之前曾經工作過。

任何幫助將大大appriciated。

+0

你能張貼您的實體和屬於查詢失敗。 – Nigel 2009-07-17 08:54:52

回答

0

原來,映射本身是正確的。 發生在對象的構造函數中的問題...

我有一個默認的無參數構造函數,它是另一個構造函數的重載。

public B() : this(null) { } 

其他建築工再有這樣的事情

public B(A c) 
{ 
    A= c; 
} 

無論哪種方式,消除了我所有的測試都通過構造器的過載:d