views:

1939

answers:

1

Hi, I've got following mappings:

<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>

And

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

Performing insert and updates are working fine (when adding and removing from the collection), but fetching the objects result always in exceptions thrown.

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.

OR...

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.

... depending on what object you're fetching. I'm sure I'm missing a simple thing here because it used to work before I implemented the cascade="all-delete-orphan".

Any help would be greatly appriciated.

A: 

It turned out that the mapping itself was correct. The problem occured in the Constructor of 1 of the objects...

I had a default parameterless constructor which was an overload to another constructor.

public B() : this(null) { }

The other constructer then had something like

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

Either way, removing the overload on the constructor made all my tests pass :D

Littlefool

related questions