NHibernate not persisting collections
Asked Answered
R

3

1

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.

Regeneracy answered 19/12, 2008 at 2:14 Comment(1)
How are you mapping the tyres to the car?Masakomasan
R
3

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.

Regeneracy answered 19/12, 2008 at 13:57 Comment(0)
B
2

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

Bustard answered 19/12, 2008 at 3:30 Comment(1)
I was having a similar problem and cascade="all" did the trick for meRubdown
T
0

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

Tremulant answered 19/12, 2008 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.