R – NHibernate not persisting collections

ado.netisessionnhibernate

I have a rather strange error with NHibernate. I am was having error with ISession been shared by across threads and got this resolved by supplying my own ADO.NET connection like:

            IDbConnection connection = new SqlConnection(ApplicationConfiguration.ConnectionString);
            connection.Open();

            ISession session = _sessionFactory.OpenSession(connection);
            session.FlushMode = FlushMode.Commit;

            return session;

My application now works but all objects with collections are been persisted in the database without their collections. for example, say a car has a list of tyres. then i create a car and then generate a list of tyres based on tyres already in the database. saving the car object will only save the car not the list!

any help on what i am doing wrong? i am using NHibernate 2.0 an i do call Session.Flush() and Transaction.Commit().

cheers.

Best Answer

hi I figured out the reason why the collections were not been persisted. my unit of work was invoking a property which returned an Isession object to persist my objects. However, this property actually returned a new ISession for each call. COnce i corrected this to use the same ISession within each unit of work, the objects were persisted properly. Thanks for all your help though.

Related Topic