views:

89

answers:

2

I have set up my mapping file and classes as suggested by many articles

class A { ... IList BBag {get;set;} ... }

class B { ... A aObject {get;set;} ... }

<class name="A">...<bag name="BBag" table="B" inverse="true" lazy="false"><key column="A_ID" /><one-to-many class="B" /></bag>...

<class name="B">...<many-to-one name="aObject" class="A" column="A_ID" />...

I added a set of A's to the A table and a set of B's to the B table, all the data is stored as expected. However if I try and access aInstance.BBag.Count I get a null reference exception. I think I missing some key knowledge on how an bag gets instantiated. Thanks

A: 

I did ARepository.Update(BOject); and when I called it back it worked for some reason I thought the update would happen automatically. Is there a way to do automatically update a bag, i.e. some mapping file setting?

Neville Chinan
A: 

When you call Save()/SaveOrUpdate()/Update() the collections of the saved object will not be instantiated/hydrated.

As such you have to select the object again if you want the collection elements. If you don't want to do that you can make a collection of the saved collection objects and assign them to the saved object's collection property.

Jaguar
I have been experimenting further today and it seems that you need to retrieve the entity again so that the collections from relatioships are retrieved. I have a set of static entities, in a helper class, that I use for unit testing and I was updating table B and trying to access the collection of B's from class A without selecting A from the database, i.e Helper.oAObject.BCollection which is always going to be empty. However oAObject = ARepo.GetById(Helper.oAObject.Id) and the collection will be available, even without an update table A. What you are saying is right, select object again.
Neville Chinan
from what you are saying, you are making a logical mistake: the BCollection of B objects has nothing to do with updating table A. Consider Collections as a handy tool to fetch objects that have a correlation (like a foreign key) to this object.
Jaguar

related questions