public void CannotBePersistedWithNonUniqueValues()
		{
			using (var session = OpenSession())
			using (session.BeginTransaction())
			{
				var e1 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Bill", Dob = new DateTime(2000, 1, 1) } };
				var e2 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Bill", Dob = new DateTime(2000, 1, 1) } };

				Assert.That(() =>
				{
					session.Save(e1);
					session.Save(e2);
					session.Flush();
				},
				Throws.Exception.TypeOf<GenericADOException>()
					.With.InnerException.TypeOf<SqlException>()
					.And.InnerException.Message.StringContaining("UNIQUE KEY"));
			}
		}
		public void CannotBePersistedWithNonUniqueValues()
		{
			using (var session = OpenSession())
			using (session.BeginTransaction())
			{
				var e1 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Bill", Dob = new DateTime(2000, 1, 1) } };
				var e2 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Bill", Dob = new DateTime(2000, 1, 1) } };

				var exception = Assert.Throws<GenericADOException>(() =>
					{
						session.Save(e1);
						session.Save(e2);
						session.Flush();
					});
				Assert.That(exception.InnerException, Is.AssignableTo<DbException>());
				Assert.That(exception.InnerException.Message,
					Is.StringContaining("unique").IgnoreCase.And.StringContaining("constraint").IgnoreCase);
			}
		}
		public void CanBePersistedWithUniqueValues()
		{
			using (var session = OpenSession())
			using (var transaction = session.BeginTransaction())
			{
				var e1 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Bill", Dob = new DateTime(2000, 1, 1) } };
				var e2 = new Employee { HireDate = DateTime.Today, Person = new Person { Name = "Hillary", Dob = new DateTime(2000, 1, 1) } };
				session.Save(e1);
				session.Save(e2);
				transaction.Commit();
			}

			using (var session = OpenSession())
			using (session.BeginTransaction())
			{
				var employees = session.Query<Employee>().ToList();
				Assert.That(employees.Count, Is.EqualTo(2));
				Assert.That(employees.Select(employee => employee.Person.Name).ToArray(), Is.EquivalentTo(new[] { "Hillary", "Bill" }));
			}
		}
예제 #4
0
		public void TestMergeComponent() 
		{
			Employee emp = null;
			IEnumerator<Employee> enumerator = null;
				
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				emp = new Employee();
				emp.HireDate = new DateTime(1999, 12, 31);
				emp.Person = new Person();
				emp.Person.Name = "steve";
				emp.Person.Dob = new DateTime(1999, 12, 31);
				s.Persist(emp);
				t.Commit();
				s.Close();
			}
			
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				emp = (Employee)s.Get(typeof(Employee), emp.Id);
				t.Commit();
				s.Close();
			}
			
			Assert.That(emp.OptionalComponent, Is.Null);
			
			emp.OptionalComponent = new OptionalComponent();
			emp.OptionalComponent.Value1 = "emp-value1";
			emp.OptionalComponent.Value2 = "emp-value2";
	
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				emp = (Employee)s.Merge(emp);
				t.Commit();
				s.Close();
			}
			
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				emp = (Employee)s.Get(typeof(Employee), emp.Id);
				t.Commit();
				s.Close();
			}
			
			Assert.That(emp.OptionalComponent.Value1, Is.EqualTo("emp-value1"));
			Assert.That(emp.OptionalComponent.Value2, Is.EqualTo("emp-value2"));

			emp.OptionalComponent.Value1 = null;
			emp.OptionalComponent.Value2 = null;
	
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				emp = (Employee)s.Merge(emp);
				t.Commit();
				s.Close();
			}
			
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				emp = (Employee)s.Get(typeof(Employee), emp.Id);
				NHibernateUtil.Initialize(emp.DirectReports);
				t.Commit();
				s.Close();
			}
			
			Assert.That(emp.OptionalComponent, Is.Null);
	
			Employee emp1 = new Employee();
			emp1.HireDate = new DateTime(1999, 12, 31);
			emp1.Person = new Person();
			emp1.Person.Name = "bozo";
			emp1.Person.Dob = new DateTime(1999, 12, 31);
			emp.DirectReports.Add(emp1);
	
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				emp = (Employee)s.Merge(emp);
				t.Commit();
				s.Close();
			}
			
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				emp = (Employee)s.Get(typeof(Employee), emp.Id);
				NHibernateUtil.Initialize(emp.DirectReports);
				t.Commit();
				s.Close();
			}
			
			Assert.That(emp.DirectReports.Count, Is.EqualTo(1));
			
			enumerator = emp.DirectReports.GetEnumerator();
			enumerator.MoveNext();
			emp1 = (Employee)enumerator.Current;
			Assert.That(emp1.OptionalComponent, Is.Null);
			
			emp1.OptionalComponent = new OptionalComponent();
			emp1.OptionalComponent.Value1 = "emp1-value1";
			emp1.OptionalComponent.Value2 = "emp1-value2";
	
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				emp = (Employee)s.Merge(emp);
				t.Commit();
				s.Close();
			}
			
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				emp = (Employee)s.Get(typeof(Employee), emp.Id);
				NHibernateUtil.Initialize(emp.DirectReports);
				t.Commit();
				s.Close();
			}
			
			Assert.That(emp.DirectReports.Count, Is.EqualTo(1));
			
			enumerator = emp.DirectReports.GetEnumerator();
			enumerator.MoveNext();
			emp1 = (Employee)enumerator.Current;
			Assert.That(emp1.OptionalComponent.Value1, Is.EqualTo("emp1-value1"));
			Assert.That(emp1.OptionalComponent.Value2, Is.EqualTo("emp1-value2"));
			
			emp1.OptionalComponent.Value1 = null;
			emp1.OptionalComponent.Value2 = null;
	
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				emp = (Employee)s.Merge(emp);
				t.Commit();
				s.Close();
			}
			
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				emp = (Employee)s.Get(typeof(Employee), emp.Id);
				NHibernateUtil.Initialize(emp.DirectReports);
				t.Commit();
				s.Close();
			}
	
			Assert.That(emp.DirectReports.Count, Is.EqualTo(1));
			
			enumerator = emp.DirectReports.GetEnumerator();
			enumerator.MoveNext();
			emp1 = (Employee)enumerator.Current;
			Assert.That(emp1.OptionalComponent, Is.Null);
	
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				s.Delete( emp );
				t.Commit();
				s.Close();
			}
		}
예제 #5
0
		public void TestComponentQueries() 
		{
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				Employee emp = new Employee();
				emp.HireDate = new DateTime(1999, 12, 31);
				emp.Person = new Person();
				emp.Person.Name = "steve";
				emp.Person.Dob = new DateTime(1999, 12, 31);
				s.Save(emp);
		
				s.CreateQuery("from Employee e where e.Person = :p and 1=1 and 2=2").SetParameter("p", emp.Person).List();
				s.CreateQuery("from Employee e where :p = e.Person").SetParameter("p", emp.Person).List();
				s.CreateQuery("from Employee e where e.Person = ('steve', current_timestamp)").List();
		
				s.Delete( emp );
				t.Commit();
				s.Close();
			}
		}
예제 #6
0
        public void TestMergeComponent()
        {
            Employee emp = null;
            IEnumerator <Employee> enumerator = null;

            using (ISession s = Sfi.OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    emp             = new Employee();
                    emp.HireDate    = new DateTime(1999, 12, 31);
                    emp.Person      = new Person();
                    emp.Person.Name = "steve";
                    emp.Person.Dob  = new DateTime(1999, 12, 31);
                    s.Persist(emp);
                    t.Commit();
                    s.Close();
                }

            using (ISession s = Sfi.OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    emp = (Employee)s.Get(typeof(Employee), emp.Id);
                    t.Commit();
                    s.Close();
                }

            Assert.That(emp.OptionalComponent, Is.Null);

            emp.OptionalComponent        = new OptionalComponent();
            emp.OptionalComponent.Value1 = "emp-value1";
            emp.OptionalComponent.Value2 = "emp-value2";

            using (ISession s = Sfi.OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    emp = (Employee)s.Merge(emp);
                    t.Commit();
                    s.Close();
                }

            using (ISession s = Sfi.OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    emp = (Employee)s.Get(typeof(Employee), emp.Id);
                    t.Commit();
                    s.Close();
                }

            Assert.That(emp.OptionalComponent.Value1, Is.EqualTo("emp-value1"));
            Assert.That(emp.OptionalComponent.Value2, Is.EqualTo("emp-value2"));

            emp.OptionalComponent.Value1 = null;
            emp.OptionalComponent.Value2 = null;

            using (ISession s = Sfi.OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    emp = (Employee)s.Merge(emp);
                    t.Commit();
                    s.Close();
                }

            using (ISession s = Sfi.OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    emp = (Employee)s.Get(typeof(Employee), emp.Id);
                    NHibernateUtil.Initialize(emp.DirectReports);
                    t.Commit();
                    s.Close();
                }

            Assert.That(emp.OptionalComponent, Is.Null);

            Employee emp1 = new Employee();

            emp1.HireDate    = new DateTime(1999, 12, 31);
            emp1.Person      = new Person();
            emp1.Person.Name = "bozo";
            emp1.Person.Dob  = new DateTime(1999, 12, 31);
            emp.DirectReports.Add(emp1);

            using (ISession s = Sfi.OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    emp = (Employee)s.Merge(emp);
                    t.Commit();
                    s.Close();
                }

            using (ISession s = Sfi.OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    emp = (Employee)s.Get(typeof(Employee), emp.Id);
                    NHibernateUtil.Initialize(emp.DirectReports);
                    t.Commit();
                    s.Close();
                }

            Assert.That(emp.DirectReports.Count, Is.EqualTo(1));

            enumerator = emp.DirectReports.GetEnumerator();
            enumerator.MoveNext();
            emp1 = (Employee)enumerator.Current;
            Assert.That(emp1.OptionalComponent, Is.Null);

            emp1.OptionalComponent        = new OptionalComponent();
            emp1.OptionalComponent.Value1 = "emp1-value1";
            emp1.OptionalComponent.Value2 = "emp1-value2";

            using (ISession s = Sfi.OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    emp = (Employee)s.Merge(emp);
                    t.Commit();
                    s.Close();
                }

            using (ISession s = Sfi.OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    emp = (Employee)s.Get(typeof(Employee), emp.Id);
                    NHibernateUtil.Initialize(emp.DirectReports);
                    t.Commit();
                    s.Close();
                }

            Assert.That(emp.DirectReports.Count, Is.EqualTo(1));

            enumerator = emp.DirectReports.GetEnumerator();
            enumerator.MoveNext();
            emp1 = (Employee)enumerator.Current;
            Assert.That(emp1.OptionalComponent.Value1, Is.EqualTo("emp1-value1"));
            Assert.That(emp1.OptionalComponent.Value2, Is.EqualTo("emp1-value2"));

            emp1.OptionalComponent.Value1 = null;
            emp1.OptionalComponent.Value2 = null;

            using (ISession s = Sfi.OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    emp = (Employee)s.Merge(emp);
                    t.Commit();
                    s.Close();
                }

            using (ISession s = Sfi.OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    emp = (Employee)s.Get(typeof(Employee), emp.Id);
                    NHibernateUtil.Initialize(emp.DirectReports);
                    t.Commit();
                    s.Close();
                }

            Assert.That(emp.DirectReports.Count, Is.EqualTo(1));

            enumerator = emp.DirectReports.GetEnumerator();
            enumerator.MoveNext();
            emp1 = (Employee)enumerator.Current;
            Assert.That(emp1.OptionalComponent, Is.Null);

            using (ISession s = Sfi.OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    s.Delete(emp);
                    t.Commit();
                    s.Close();
                }
        }