NHibernate collections not persisted
I have a rather strange error with NHibernate. I had a bug with ISession that was split between threads and solved by supplying my own ADO.NET connection, like this:
IDbConnection connection = new SqlConnection(ApplicationConfiguration.ConnectionString);
connection.Open();
ISession session = _sessionFactory.OpenSession(connection);
session.FlushMode = FlushMode.Commit;
return session;
Now my application works, but all objects with collections are saved in the database without their collections. for example, a car is said to have a tire list. then I create a car and then I create a list of tires based on tires already in the database. saving the vehicle object will save the vehicle not in the list!
any help on what i am doing wrong? I am using NHibernate 2.0 and I am calling Session.Flush () and Transaction.Commit ().
greetings.
Hi I figured out the reason why the collections were not saved. my part of the job was calling a property that returned an Isession object to persist my objects. However, this property actually returned a new ISession for each call. COnce I fixed this to use the same ISession in every piece of work, the objects were saved properly. Thank you for your help.
Check the cascade attribute for your collection mapping. By default, this value is "none", which means that child objects must be explicitly saved. You probably want cascade = "all" or cascade = "all-delete-orphan".
Are you using NHibernate.ISession.save (object) before flashing and committing the bus list?