Esempio n. 1
0
        public void get_cheapest_supplier_for_product()
        {
            var product = new ProductBuilder().Build();
            var supplier1 = new SupplierBuilder().Build();
            var supplier2 = new SupplierBuilder().Build();
            var supplier3 = new SupplierBuilder().Build();
            product.AddSource(supplier1, 50);
            product.AddSource(supplier2, 48);
            product.AddSource(supplier3, 52);
            Session.Save(product);
            Session.Flush();

            var cheapestSupplier =
                Session.CreateQuery(
                    "select ps.Supplier from ProductSource ps where ps.Product.Id = :productId order by ps.Cost asc")
                    .SetParameter("productId", product.Id)
                    .SetMaxResults(1)
                    .UniqueResult<Supplier>();

            // the following LINQ approach also works, but uses an uneccessary join on Supplier in the subselect!
            //var cheapestSupplierId = Session.Query<ProductSource>()
            //    .Where(s => s.Product.Id == product.Id)
            //    .OrderBy(s => s.Cost)
            //    .Select(s => s.Supplier.Id)
            //    .Take(1);

            //var cheapestSupplier = Session.Query<Supplier>()
            //    .Where(s => s.Id == cheapestSupplierId.Single())
            //    .Single();

            Assert.AreEqual(supplier2, cheapestSupplier);
        }
 public void attaching_a_transient_object_does_not_hit_the_database_until_the_session_is_flushed()
 {
     var product = new ProductBuilder().Build();
     // of course, it already has an ID value since it's been assigned in the Product constructor
     Assert.AreNotEqual(Guid.Empty, product.Id);
     var insertCount = Statistics.EntityInsertCount;
     Session.Save(product);
     // 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 insert_hits_database_immediately()
        {
            using (var session = SessionFactory.OpenStatelessSession())
            using (var transaction = session.BeginTransaction())
            {
#if HBMSQLSERVER || FLUENTSQLSERVER
                session.SetBatchSize(0);
#endif
                var product = new ProductBuilder().Build();
                session.Insert(product);
                Logger.Info("the insert statement has been sent to the database before the session is flushed");
                transaction.Commit();
            }
        }
 public void inserts_are_batched()
 {
     using (var session = SessionFactory.OpenStatelessSession())
     using (var transaction = session.BeginTransaction())
     {
         session.SetBatchSize(2);
         var builder = new ProductBuilder();
         session.Insert(builder.Build());
         session.Insert(builder.Build());
         Logger.Info("the first batch of 2 inserts has been sent to the database now");
         session.Insert(builder.Build());
         session.Insert(builder.Build());
         Logger.Info("the second batch of 2 inserts has been sent to the database now, without flushing the session");
         transaction.Commit();
     }
 }
Esempio n. 5
0
        public void get_cheapest_supplier_for_product()
        {
            var product = new ProductBuilder().Build();
            var supplier1 = new SupplierBuilder().Build();
            var supplier2 = new SupplierBuilder().Build();
            var supplier3 = new SupplierBuilder().Build();
            product.AddSource(supplier1, 50);
            product.AddSource(supplier2, 48);
            product.AddSource(supplier3, 52);
            Session.Save(product);
            Session.Flush();

            Supplier cheapestSupplier = null;

            Assert.AreEqual(supplier2, cheapestSupplier);
        }
        public void no_dirty_checking()
        {
            var product = new ProductBuilder().Build();

            using (var session = SessionFactory.OpenStatelessSession())
            using (var transaction = session.BeginTransaction())
            {
                session.Insert(product);
                product.Name = product.Name + "2";
                // with a normal NHibernate session, the changed name would've been detected by NHibernate
                // and an update statement would've been sent
                transaction.Commit();
            }

            using (var session = SessionFactory.OpenSession())
            {
                Assert.AreNotEqual(product.Name, session.Get<Product>(product.Id).Name);
            }
        }
 public void retrieving_the_same_entity_twice_hits_the_database_twice()
 {
     using (var session = SessionFactory.OpenStatelessSession())
     {
         var product = new ProductBuilder().Build();
         session.Insert(product);
         var firstRetrieval = session.Get<Product>(product.Id);
         Logger.Info("this issues a select statement, which shows that there is no session cache");
         var secondRetrieval = session.Get<Product>(product.Id);
         Logger.Info("this issues a second select statement...");
     }
 }
Esempio n. 8
0
        public void where_string_property_matches_value_exactly()
        {
            var product4 = new ProductBuilder().WithName("blah product 2 blah").Build();
            Session.Save(product4);
            Flush();

            var products = Session.QueryOver<Product>()
                .WhereRestrictionOn(p => p.Name).IsLike("product 2", MatchMode.Exact)
                .List();

            Assert.IsFalse(products.Contains(_product1));
            Assert.IsTrue(products.Contains(_product2));
            Assert.IsFalse(products.Contains(_product3));
            Assert.IsFalse(products.Contains(product4));
            products.Each(p => Assert.AreEqual("product 2", p.Name));
        }
Esempio n. 9
0
        public void where_string_property_matches_value_exactly()
        {
            var product4 = new ProductBuilder().WithName("blah product 2 blah").Build();
            Session.Save(product4);
            Flush();

            var products = Session.CreateQuery("from Product p where p.Name = :value")
                .SetParameter("value", "product 2")
                .List<Product>();

            Assert.IsFalse(products.Contains(_product1));
            Assert.IsTrue(products.Contains(_product2));
            Assert.IsFalse(products.Contains(_product3));
            Assert.IsFalse(products.Contains(product4));
            products.Each(p => Assert.AreEqual("product 2", p.Name));
        }
Esempio n. 10
0
        public void where_string_property_matches_value_exactly()
        {
            var product4 = new ProductBuilder().WithName("blah product 2 blah").Build();
            Session.Save(product4);
            Flush();

            var products = Session.CreateCriteria<Product>()
                .Add(Restrictions.Like("Name", "product 2", MatchMode.Exact))
                .List<Product>();

            Assert.IsFalse(products.Contains(_product1));
            Assert.IsTrue(products.Contains(_product2));
            Assert.IsFalse(products.Contains(_product3));
            Assert.IsFalse(products.Contains(product4));
            products.Each(p => Assert.AreEqual("product 2", p.Name));
        }