Exemplo n.º 1
0
 public void attaching_a_transient_object_immediately_hits_the_database()
 {
     var employee = new EmployeeBuilder().Build();
     var insertCount = Statistics.EntityInsertCount;
     Assert.AreEqual(employee.Id, 0);
     Session.Save(employee);
     Assert.Greater(employee.Id, 0);
     // a new insert statement will have been issued already even though we haven't flushed the session
     Assert.AreEqual(insertCount + 1, Statistics.EntityInsertCount);
 }
Exemplo n.º 2
0
        public void get_employee_with_highest_salary()
        {
            // the maximum salary to be generated for the testdata is set at 3500
            var topPaidEmployee = new EmployeeBuilder().WithSalary(3600).Build();
            Session.Save(topPaidEmployee);
            FlushAndClear();

            Employee retrievedEmployee = null;

            Assert.AreEqual(topPaidEmployee, retrievedEmployee);
        }
Exemplo n.º 3
0
        public void get_employee_with_highest_salary()
        {
            // the maximum salary to be generated for the testdata is set at 3500
            var topPaidEmployee = new EmployeeBuilder().WithSalary(3600).Build();
            Session.Save(topPaidEmployee);
            FlushAndClear();

            var retrievedEmployee = Session.Query<Employee>()
                .OrderByDescending(e => e.Salary)
                .First();

            Assert.AreEqual(topPaidEmployee, retrievedEmployee);
        }
Exemplo n.º 4
0
        public void fetch_inner_join_on_an_optional_many_to_one()
        {
            var employeeWithoutManager = new EmployeeBuilder().Build();
            Session.Save(employeeWithoutManager);

            var employeeWithManager = new EmployeeBuilder().WithManager(employeeWithoutManager).Build();
            Session.Save(employeeWithManager);

            FlushAndClear();

            var employeesWithManagers = Session.CreateQuery("from Employee e join fetch e.Manager").List<Employee>();

            var retrievedEmployeeWithoutManager = employeesWithManagers.FirstOrDefault(e => e.Id == employeeWithoutManager.Id);
            var retrievedEmployeeWithManager = employeesWithManagers.FirstOrDefault(e => e.Id == employeeWithManager.Id);
            Assert.IsNotNull(retrievedEmployeeWithManager);
            Assert.IsNull(retrievedEmployeeWithoutManager);
            employeesWithManagers.Each(e => Assert.IsTrue(NHibernateUtil.IsInitialized(e.Manager)));
        }
Exemplo n.º 5
0
        public void fetch_join_on_a_many_to_one_with_explicit_inner_join()
        {
            var employeeWithoutManager = new EmployeeBuilder().Build();
            Session.Save(employeeWithoutManager);

            var employeeWithManager = new EmployeeBuilder().WithManager(employeeWithoutManager).Build();
            Session.Save(employeeWithManager);

            FlushAndClear();

            var employeesWithManagers = Session.CreateCriteria<Employee>()
                .CreateCriteria("Manager", JoinType.InnerJoin)
                .List<Employee>();

            var retrievedEmployeeWithoutManager = employeesWithManagers.FirstOrDefault(e => e.Id == employeeWithoutManager.Id);
            var retrievedEmployeeWithManager = employeesWithManagers.FirstOrDefault(e => e.Id == employeeWithManager.Id);
            Assert.IsNotNull(retrievedEmployeeWithManager);
            Assert.IsNull(retrievedEmployeeWithoutManager);
            employeesWithManagers.Each(e => Assert.IsTrue(NHibernateUtil.IsInitialized(e.Manager)));
        }
Exemplo n.º 6
0
        public void fetch_join_on_optional_many_to_one_with_explicit_outer_join()
        {
            var employeeWithoutManager = new EmployeeBuilder().Build();
            Session.Save(employeeWithoutManager);

            var employeeWithManager = new EmployeeBuilder().WithManager(employeeWithoutManager).Build();
            Session.Save(employeeWithManager);

            FlushAndClear();

            var employeesWithOrWithoutManagers = Session.QueryOver<Employee>()
                .JoinQueryOver(e => e.Manager, JoinType.LeftOuterJoin)
                .List();

            var retrievedEmployeeWithoutManager = employeesWithOrWithoutManagers.FirstOrDefault(e => e.Id == employeeWithoutManager.Id);
            var retrievedEmployeeWithManager = employeesWithOrWithoutManagers.FirstOrDefault(e => e.Id == employeeWithManager.Id);
            Assert.IsNotNull(retrievedEmployeeWithManager);
            Assert.IsNotNull(retrievedEmployeeWithoutManager);
            Assert.IsNull(retrievedEmployeeWithoutManager.Manager);
            Assert.IsTrue(NHibernateUtil.IsInitialized(retrievedEmployeeWithManager.Manager));
        }
Exemplo n.º 7
0
        public void fetch_join_on_optional_many_to_one_does_not_do_implicit_outer_join()
        {
            var employeeWithoutManager = new EmployeeBuilder().Build();
            Session.Save(employeeWithoutManager);

            var employeeWithManager = new EmployeeBuilder().WithManager(employeeWithoutManager).Build();
            Session.Save(employeeWithManager);

            FlushAndClear();

            var employeesWithOrWithoutManagers = Session.QueryOver<Employee>()
                .JoinQueryOver(e => e.Manager)
                .List();

            var retrievedEmployeeWithoutManager = employeesWithOrWithoutManagers.FirstOrDefault(e => e.Id == employeeWithoutManager.Id);
            var retrievedEmployeeWithManager = employeesWithOrWithoutManagers.FirstOrDefault(e => e.Id == employeeWithManager.Id);
            Assert.IsNotNull(retrievedEmployeeWithManager);
            // retrievedEmployeeWithoutManager would not be null if an outer join was used
            Assert.IsNull(retrievedEmployeeWithoutManager);
            employeesWithOrWithoutManagers.Each(e => Assert.IsTrue(NHibernateUtil.IsInitialized(e.Manager)));
        }
Exemplo n.º 8
0
        public void fetch_join_on_a_many_to_one_with_implicit_outer_join()
        {
            var employeeWithoutManager = new EmployeeBuilder().Build();
            Session.Save(employeeWithoutManager);

            var employeeWithManager = new EmployeeBuilder().WithManager(employeeWithoutManager).Build();
            Session.Save(employeeWithManager);

            FlushAndClear();

            var employeesWithOrWithoutManagers = Session.CreateCriteria<Employee>()
                .SetFetchMode("Manager", FetchMode.Join) // implicit outer join because Manager is an optional property
                .List<Employee>();

            var retrievedEmployeeWithoutManager = employeesWithOrWithoutManagers.FirstOrDefault(e => e.Id == employeeWithoutManager.Id);
            var retrievedEmployeeWithManager = employeesWithOrWithoutManagers.FirstOrDefault(e => e.Id == employeeWithManager.Id);
            Assert.IsNotNull(retrievedEmployeeWithManager);
            Assert.IsNotNull(retrievedEmployeeWithoutManager);
            Assert.IsNull(retrievedEmployeeWithoutManager.Manager);
            Assert.IsTrue(NHibernateUtil.IsInitialized(retrievedEmployeeWithManager.Manager));
        }
Exemplo n.º 9
0
        public void fetch_join_on_an_optional_many_to_one_and_emulate_inner_join()
        {
            var employeeWithoutManager = new EmployeeBuilder().Build();
            Session.Save(employeeWithoutManager);

            var employeeWithManager = new EmployeeBuilder().WithManager(employeeWithoutManager).Build();
            Session.Save(employeeWithManager);

            FlushAndClear();

            var employeesWithManagers = Session.Query<Employee>()
                .Where(e => e.Manager != null)
                .Fetch(e => e.Manager)
                .ToList();

            var retrievedEmployeeWithoutManager = employeesWithManagers.FirstOrDefault(e => e.Id == employeeWithoutManager.Id);
            var retrievedEmployeeWithManager = employeesWithManagers.FirstOrDefault(e => e.Id == employeeWithManager.Id);
            Assert.IsNotNull(retrievedEmployeeWithManager);
            Assert.IsNull(retrievedEmployeeWithoutManager);
            employeesWithManagers.Each(e => Assert.IsTrue(NHibernateUtil.IsInitialized(e.Manager)));
        }
Exemplo n.º 10
0
        public void fetch_join_on_an_optional_many_to_one()
        {
            var employeeWithoutManager = new EmployeeBuilder().Build();
            Session.Save(employeeWithoutManager);

            var employeeWithManager = new EmployeeBuilder().WithManager(employeeWithoutManager).Build();
            Session.Save(employeeWithManager);

            FlushAndClear();

            var employeesWithOrWithoutManagers = Session.Query<Employee>()
                .Fetch(e => e.Manager) // again... outer join by default
                .ToList();

            var retrievedEmployeeWithoutManager = employeesWithOrWithoutManagers.FirstOrDefault(e => e.Id == employeeWithoutManager.Id);
            var retrievedEmployeeWithManager = employeesWithOrWithoutManagers.FirstOrDefault(e => e.Id == employeeWithManager.Id);
            Assert.IsNotNull(retrievedEmployeeWithManager);
            Assert.IsNotNull(retrievedEmployeeWithoutManager);
            Assert.IsNull(retrievedEmployeeWithoutManager.Manager);
            Assert.IsTrue(NHibernateUtil.IsInitialized(retrievedEmployeeWithManager.Manager));
        }