EDIT: Deleted the original question. After reading, I realised that I would have to paste so much additional code that it would take someone a couple of days just to understand what was happening in my own code that it may detract from the issue at hand.

Problem: When I add an object to a collection (many-to-many relationship) the object is saved and committed to the database. However, if I then try and retrieve that object somewhere else (on a different thread) the object state is incorrect and the collection does not contain the recently added item.

Checking the database shows that the item has been added successfully.

Does anyone know what would cause NHibernate to do this? Googling revealed that it's generally a problem with not having an Inverse relationship. However, I've tried with and without and inverse map and the problem still occurs.

Also, the problem ONLY occurs if I attempt to retrieve data from a different object/method than the one that originally saved the data.

I would post code, but it really wouldn't be helpful and I would have to post about 1,000+ lines just so that you could get the context. (The Domain Objects, the Interfaces, the NHibernate Data Access Objects, the Service Code, the Test Code etc.)

There is a lot ^^

Solved: This was caused by an inconsistency with the NHibernate Session Management.

The sessions use a caching mode. With Unit Testing a separate instance of the app is started so the sessions don't work as expected of a single application.

Taking this into account I re-created the order in which sessions are created and closed and it corrected the issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.