public void Avoiding_Select_N_Plus_One_Issue()
        {
            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                for (int i = 0; i < 10; i++)
                {
                    var customer = new CustomerWithOneToMany {
                        Name = string.Format("Customer: {0}", i)
                    };
                    unitOfWork.Session.Save(customer);

                    var order = new OrderWithManyToOne {
                        Name = "Order"
                    };
                    customer.AddOrder(order);
                }

                unitOfWork.Commit();
            }

            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                // HQL (Hibernate Query Language)
                var customers = unitOfWork.Session.CreateQuery("from CustomerWithOneToMany customer left join fetch customer.Orders")
                                .List <CustomerWithOneToMany>();

                // Criteria Api
                unitOfWork.Session.Evict(customers);
                customers = unitOfWork.Session.CreateCriteria <CustomerWithOneToMany>()
                            .SetFetchMode("Orders", FetchMode.Eager)
                            .List <CustomerWithOneToMany>();

                // QueryOver
                unitOfWork.Session.Evict(customers);
                customers = unitOfWork.Session.QueryOver <CustomerWithOneToMany>()
                            .Fetch(x => x.Orders).Eager
                            .List();

                // Linq
                unitOfWork.Session.Evict(customers);
                customers = unitOfWork.Session.Query <CustomerWithOneToMany>()
                            .Fetch(x => x.Orders)
                            .ToList();

                foreach (var customer in customers)
                {
                    foreach (var order in customer.Orders)
                    {
                        var name = order.Name;
                    }
                }
            }

            Assert.Equal(20, _sessionFactory.Statistics.EntityInsertCount);
        }
예제 #2
0
        public void Avoiding_Select_N_Plus_One_Issue()
        {
            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                for (int i = 0; i < 10; i++)
                {
                    var customer = new CustomerWithOneToMany { Name = string.Format("Customer: {0}", i) };
                    unitOfWork.Session.Save(customer);

                    var order = new OrderWithManyToOne { Name = "Order" };
                    customer.AddOrder(order);
                }

                unitOfWork.Commit();
            }

            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                // HQL (Hibernate Query Language)
                var customers = unitOfWork.Session.CreateQuery("from CustomerWithOneToMany customer left join fetch customer.Orders")
                    .List<CustomerWithOneToMany>();

                // Criteria Api
                unitOfWork.Session.Evict(customers);
                customers = unitOfWork.Session.CreateCriteria<CustomerWithOneToMany>()
                    .SetFetchMode("Orders", FetchMode.Eager)
                    .List<CustomerWithOneToMany>();

                // QueryOver
                unitOfWork.Session.Evict(customers);
                customers = unitOfWork.Session.QueryOver<CustomerWithOneToMany>()
                    .Fetch(x => x.Orders).Eager
                    .List();

                // Linq
                unitOfWork.Session.Evict(customers);
                customers = unitOfWork.Session.Query<CustomerWithOneToMany>()
                    .Fetch(x => x.Orders)
                    .ToList();

                foreach (var customer in customers)
                {
                    foreach (var order in customer.Orders)
                    {
                        var name = order.Name;
                    }
                }
            }

            Assert.Equal(20, _sessionFactory.Statistics.EntityInsertCount);
        }
        public void Common_Select_N_Plus_One_Issue()
        {
            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                for (int i = 0; i < 10; i++)
                {
                    var customer = new CustomerWithOneToMany {
                        Name = string.Format("Customer: {0}", i)
                    };
                    unitOfWork.Session.Save(customer);

                    var order = new OrderWithManyToOne {
                        Name = "Order"
                    };
                    customer.AddOrder(order);
                }

                unitOfWork.Commit();
            }

            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                var customers = unitOfWork.Session.QueryOver <CustomerWithOneToMany>().List();
                foreach (var customer in customers)
                {
                    foreach (var order in customer.Orders)
                    {
                        var name = order.Name;
                    }
                }

                unitOfWork.Commit();
            }

            Assert.Equal(20, _sessionFactory.Statistics.EntityInsertCount);
        }
        public void Can_Customer_Add_Orders_In_A_One_To_Many_bidirectional_Association()
        {
            // Forgetting to set the inverse true on CustomerWithOneToMany will cause unneccessary update statements

            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                var customer = new CustomerWithOneToMany {
                    Name = "Customer"
                };
                unitOfWork.Session.Save(customer);

                for (int i = 0; i < 10; i++)
                {
                    var order = new OrderWithManyToOne {
                        Name = string.Format("Order: {0}", i)
                    };
                    customer.AddOrder(order);
                }

                unitOfWork.Commit();
            }

            Assert.Equal(11, _sessionFactory.Statistics.EntityInsertCount);
        }
예제 #5
0
        public void Common_Select_N_Plus_One_Issue()
        {
            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                for (int i = 0; i < 10; i++)
                {
                    var customer = new CustomerWithOneToMany { Name = string.Format("Customer: {0}", i) };
                    unitOfWork.Session.Save(customer);

                    var order = new OrderWithManyToOne { Name = "Order" };
                    customer.AddOrder(order);
                }

                unitOfWork.Commit();
            }

            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                var customers = unitOfWork.Session.QueryOver<CustomerWithOneToMany>().List();
                foreach (var customer in customers)
                {
                    foreach (var order in customer.Orders)
                    {
                        var name = order.Name;
                    }
                }

                unitOfWork.Commit();
            }

            Assert.Equal(20, _sessionFactory.Statistics.EntityInsertCount);
        }
예제 #6
0
        public void Can_Customer_Add_Orders_In_A_One_To_Many_bidirectional_Association()
        {
            // Forgetting to set the inverse true on CustomerWithOneToMany will cause unneccessary update statements

            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                var customer = new CustomerWithOneToMany { Name = "Customer" };
                unitOfWork.Session.Save(customer);

                for (int i = 0; i < 10; i++)
                {
                    var order = new OrderWithManyToOne { Name = string.Format("Order: {0}", i) };
                    customer.AddOrder(order);
                }

                unitOfWork.Commit();
            }

            Assert.Equal(11, _sessionFactory.Statistics.EntityInsertCount);
        }