title: Difference between ISession.Save/Update and ITransaction.Commit in NHibernate
body: <p>I have a piece of code that adds elements to entity collection (one-to-many relation). This is the version with <strong>ISession.Save</strong></p>
  
  <pre><code>        using (ISession session = sessionFactory.OpenSession())
          {
              var package = session.QueryOver&lt;Package&gt;().Where(x =&gt; x.ID == selectedPackage).SingleOrDefault();
              foreach(var themeId in selectedThemes)
              {
                  var selectedTheme = session.QueryOver&lt;HBTheme&gt;().Where(x =&gt; x.ID == themeId).SingleOrDefault();
                  if (selectedTheme != null)
                  {
                      package.Themes.Add(new PackageTheme() { Package = package, Theme = selectedTheme });
                  }
              }
              session.Save(package);
          }
  </code></pre>
  
  <p>and that version didn't work for me. As I had test written with <strong>ITransaction</strong>, I changed it a little to the following:</p>
  
  <pre><code>        using (ISession session = sessionFactory.OpenSession())
          using (ITransaction transaction = session.BeginTransaction())
          {
              var package = session.QueryOver&lt;Package&gt;().Where(x =&gt; x.ID == selectedPackage).SingleOrDefault();
              foreach(var themeId in selectedThemes)
              {
                  var selectedTheme = session.QueryOver&lt;HBTheme&gt;().Where(x =&gt; x.ID == themeId).SingleOrDefault();
                  if (selectedTheme != null)
                  {
                      package.Themes.Add(new PackageTheme() { Package = package, Theme = selectedTheme });
                  }
              }
              transaction.Commit();
          }
  </code></pre>
  
  <p>and now it works. Elements in <strong>package.Themes</strong> collection are stored in database. How come? Thanks!</p>