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.

+1


source to share


3 answers


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.



+3


source


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



+2


source


Are you using NHibernate.ISession.save (object) before flashing and committing the bus list?

0


source







All Articles