public void proxies_can_be_used_to_set_foreign_keys_without_loading_the_actual_entity()
        {
            var customer = new CustomerBuilder().Build();
            Session.Save(customer);
            FlushAndClear();

            var customerProxy = Session.Load<Customer>(customer.Id);
            Logger.Info("there was no select statement to retrieve the customer");
            var order = new OrderBuilder().WithCustomer(customerProxy).Build();
            Logger.Info("we can now insert the order without having to retrieve the customer");
            Session.Save(order.Employee);
            Session.Save(order);
            Flush();
        }
 public void attaching_a_transient_object_does_not_hit_the_database_until_the_session_is_flushed()
 {
     var order = new OrderBuilder().Build();
     Session.Save(order.Employee); // there's no cascade set on Order.Employee
     Session.Save(order.Customer); // saving the customer already as well, so we can flush these changes already
     Session.Flush();
     Assert.AreEqual(Guid.Empty, order.Id);
     var insertCount = Statistics.EntityInsertCount;
     Session.Save(order);
     Assert.AreNotEqual(Guid.Empty, order.Id);
     // the insert hasn't been issued yet
     Assert.AreEqual(insertCount, Statistics.EntityInsertCount);
     // this will trigger the insert
     Session.Flush();
     Assert.AreEqual(insertCount + 1, Statistics.EntityInsertCount);
 }
        public void many_to_one_properties_are_proxies_by_default()
        {
            var order = new OrderBuilder().Build();
            Session.Save(order.Employee);
            Session.Save(order);
            FlushAndClear();

            var retrievedOrder = Session.Get<Order>(order.Id);
            Assert.IsFalse(NHibernateUtil.IsInitialized(retrievedOrder.Customer));
            Assert.IsFalse(NHibernateUtil.IsInitialized(retrievedOrder.Employee));
            Logger.Info("if we access the Id values of both many-to-one properties, no select statement will be issued");
            Logger.Info(string.Format("Id of order.Customer: {0}", retrievedOrder.Customer.Id));
            Logger.Info(string.Format("Id of order.Employee: {0}", retrievedOrder.Employee.Id));
            Logger.Info("but once we access other properties, it does issue the select statement(s)");
            Logger.Info(string.Format("Name of order.Customer: {0}", retrievedOrder.Customer.Name));
            Logger.Info(string.Format("LastName of order.Employee: {0}", retrievedOrder.Employee.LastName));
        }
 public void proxies_cant_be_used_to_set_foreign_keys_with_values_that_dont_exist()
 {
     var nonExistingCustomerProxy = Session.Load<Customer>(9999);
     var order = new OrderBuilder().WithCustomer(nonExistingCustomerProxy).Build();
     Logger.Info("we can now insert the order without having to retrieve the customer");
     Session.Save(order.Employee);
     Session.Save(order);
     Assert.Throws<NHibernate.Exceptions.GenericADOException>(Flush);
 }