views:

314

answers:

4

Hi,

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.

+2  A: 

Check out the cascade attribute on your collection mapping - by default this is set to 'none', meaning child entities need to be explicitly saved. You probably want cascade="all" or cascade="all-delete-orphan".

Sam
I was having a similar problem and cascade="all" did the trick for me
tjjjohnson
A: 

thanks Sam. I tried the cascade thing but the collections are still not persisted.

A: 

are you using NHibernate.ISession.save(object) before the flush and commit of the tyres list?

Phil Corcoran
+2  A: 

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.