title: Entities are not persisted after creation
body: <p>Disclaimer: this is my first project using NHibernate, I may miss something "obvious".</p>
  
  <p>My problem is as follow. I have a command that creates an order:</p>
  
  <pre><code>public void Handle(BuyOneProductCommand command)
  {
      var product = _uow.Query&lt;Product&gt;().Single(p =&gt; p.Name == command.ProductName);
      var order = new Order(product, 1);
      _uow.SaveOrUpdate(order);
      _uow.Commit();
  }
  </code></pre>
  
  <p>The command receives a freshly created <code>UnitOfWork</code>. However after <code>Commit()</code> my changes are never persisted to the database and no exception thrown. Any thoughts why ?</p>
  
  <p>Order</p>
  
  <pre><code>public class Order : AggregateRoot
  {
      public virtual Product Product { get; }
      public virtual int Quantity { get; }
      public virtual DateTime Date { get; }
  
      public Order(Product product, int quantity) : this()
      {
          Product = product;
          Quantity = quantity;
          Date = DateTime.Now;
          AddDomainEvent(new OrderCreatedEvent(this));
      }
  
      protected Order() { }
  }
  </code></pre>
  
  <p>OrderMap</p>
  
  <pre><code>public class OrderMap : ClassMap&lt;Order&gt;
  {
      public OrderMap()
      {
          Id(x =&gt; x.Id);
          References(o =&gt; o.Product);
          Map(x =&gt; x.Quantity);
          Map(x =&gt; x.Date);
      }
  }
  </code></pre>
  
  <p>UnitOfWork</p>
  
  <pre><code>public sealed class UnitOfWork : IUnitOfWork
  {
      private readonly ISession _session;
      private readonly ITransaction _transaction;
      private bool _isAlive = true;
  
      public UnitOfWork()
      {
          _session = SessionFactory.OpenSession();
          _transaction = _session.BeginTransaction(IsolationLevel.ReadCommitted);
      }
  
      public void Commit()
      {
          if (!_isAlive)
              return;
  
          try
          {
              _transaction.Commit();
          }
          finally
          {
              _isAlive = false;
              _transaction.Dispose();
              _session.Dispose();
          }
      }
  
      public T Get&lt;T&gt;(long id) where T : class
      {
          return _session.Get&lt;T&gt;(id);
      }
  
      public void SaveOrUpdate&lt;T&gt;(T entity)
      {
          _session.SaveOrUpdate(entity);
      }
  
      public IQueryable&lt;T&gt; Query&lt;T&gt;()
      {
          return _session.Query&lt;T&gt;();
      }
  }
  </code></pre>
  
  <p>SessionFactory</p>
  
  <pre><code>public static class SessionFactory
  {
      private static ISessionFactory _factory;
  
      public static void Init(string connectionString)
      {
          _factory = BuildSessionFactory(connectionString);
      }
  
      public static ISession OpenSession()
      {
          return _factory.OpenSession();
      }
  
      private static ISessionFactory BuildSessionFactory(string connectionString)
      {
          return Fluently.Configure()
              .Database(SQLiteConfiguration.Standard.ConnectionString(connectionString).ShowSql())
              .Mappings(m =&gt; m.FluentMappings
                  .AddFromAssembly(Assembly.GetExecutingAssembly())
                  .Conventions.Add(
                      ForeignKey.EndsWith("Id"),
                      ConventionBuilder.Property.When(criteria =&gt; criteria.Expect(x =&gt; x.Nullable, Is.Not.Set), x =&gt; x.Not.Nullable()))
                  .Conventions.Add&lt;OtherConventions&gt;()
                  .Conventions.Add&lt;IncrementConvention&gt;()
              )
              .ExposeConfiguration(x =&gt;
              {
                  x.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[] { new DomainEventsListener() };
                  x.EventListeners.PreInsertEventListeners = new IPreInsertEventListener[] { new DomainEventsListener() };
                  x.EventListeners.PreDeleteEventListeners = new IPreDeleteEventListener[] { new DomainEventsListener() };
                  x.EventListeners.PreCollectionUpdateEventListeners = new IPreCollectionUpdateEventListener[] { new DomainEventsListener() };
                  x.EventListeners.PostCommitUpdateEventListeners = new IPostUpdateEventListener[] { new IntegrationEventsListener() };
                  x.EventListeners.PostCommitInsertEventListeners = new IPostInsertEventListener[] { new IntegrationEventsListener() };
                  x.EventListeners.PostCommitDeleteEventListeners = new IPostDeleteEventListener[] { new IntegrationEventsListener() };
                  x.EventListeners.PostCollectionUpdateEventListeners = new IPostCollectionUpdateEventListener[] { new IntegrationEventsListener() };
                  new SchemaUpdate(x).Execute(true, true);
              })
              .BuildSessionFactory();
      }
  
      private class OtherConventions : IHasManyConvention, IReferenceConvention
      {
          public void Apply(IOneToManyCollectionInstance instance)
          {
              instance.LazyLoad();
              instance.AsBag();
              instance.Cascade.SaveUpdate();
              instance.Inverse();
          }
  
          public void Apply(IManyToOneInstance instance)
          {
              instance.LazyLoad(Laziness.Proxy);
              instance.Cascade.None();
              instance.Not.Nullable();
          }
      }
  
      private class IncrementConvention : IIdConvention
      {
          public void Apply(IIdentityInstance instance)
          {
              instance.Column("Id");
              instance.GeneratedBy.Increment();
          }
      }
  }
  </code></pre>
  
  <p>IPreInsertEventListener</p>
  
  <pre><code>public bool OnPreInsert(PreInsertEvent @event)
  {
      var aggregateRoot = @event.Entity as AggregateRoot;
      if (aggregateRoot == null)
          return true;
  
      foreach (IEvent domainEvent in aggregateRoot.DomainEvents)
      {
          DomainEvents.Dispatch(domainEvent);
      }
  
      aggregateRoot.ClearDomainEvents();
  
      return true;
  }
  </code></pre>