2009-07-16 30 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