Exemplo n.º 1
0
		public void TestUpdateFalse() 
		{
			User u;
			
			sessions.Statistics.Clear();
				
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				u = new User("gavin", "secret", new Person("Gavin King", new DateTime(1999, 12, 31), "Karbarook Ave"));
				s.Persist(u);
				s.Flush();
				u.Person.Name = "XXXXYYYYY";
				t.Commit();
				s.Close();
			}
			
			Assert.That(sessions.Statistics.EntityInsertCount, Is.EqualTo(1));
			Assert.That(sessions.Statistics.EntityUpdateCount, Is.EqualTo(0));

			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				u = (User)s.Get(typeof(User), "gavin");
				Assert.That(u.Person.Name, Is.EqualTo("Gavin King"));
				s.Delete(u);
				t.Commit();
				s.Close();
			}
			
			Assert.That(sessions.Statistics.EntityDeleteCount, Is.EqualTo(1));
		}
Exemplo n.º 2
0
		public void TestComponent() 
		{
			User u;
			
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				u = new User("gavin", "secret", new Person("Gavin King", new DateTime(1999, 12, 31), "Karbarook Ave"));
				s.Persist(u);
				s.Flush();
				u.Person.ChangeAddress("Phipps Place");
				t.Commit();
			}
			
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{			
				u = (User)s.Get(typeof(User), "gavin");
				Assert.That(u.Person.Address, Is.EqualTo("Phipps Place"));
				Assert.That(u.Person.PreviousAddress, Is.EqualTo("Karbarook Ave"));
				Assert.That(u.Person.Yob, Is.EqualTo(u.Person.Dob.Year));
				u.Password = "******";
				t.Commit();
			}
			
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				u = (User)s.Get(typeof(User), "gavin");
				Assert.That(u.Person.Address, Is.EqualTo("Phipps Place"));
				Assert.That(u.Person.PreviousAddress, Is.EqualTo("Karbarook Ave"));
				Assert.That(u.Password, Is.EqualTo("$ecret"));
				s.Delete(u);
				t.Commit();
			}
		}
Exemplo n.º 3
0
		public void TestCustomColumnReadAndWrite() 
		{
			const double HEIGHT_INCHES = 73;
			const double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d;
			
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				User u = new User("steve", "hibernater", new Person( "Steve Ebersole", new DateTime(1999, 12, 31), "Main St"));
				u.Person.HeightInches = HEIGHT_INCHES;
				s.Persist(u);
				s.Flush();
			
				// Test value conversion during insert
				double heightViaSql = (double)s.CreateSQLQuery("select height_centimeters from t_user where t_user.username='******'").UniqueResult();
				Assert.That(heightViaSql, Is.EqualTo(HEIGHT_CENTIMETERS).Within(0.01d));
		
				// Test projection
				double heightViaHql = (double)s.CreateQuery("select u.Person.HeightInches from User u where u.Id = 'steve'").UniqueResult();
				Assert.That(heightViaHql, Is.EqualTo(HEIGHT_INCHES).Within(0.01d));
				
				// Test restriction and entity load via criteria
				u = (User)s.CreateCriteria(typeof(User))
					.Add(Restrictions.Between("Person.HeightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d))
					.UniqueResult();
				Assert.That(u.Person.HeightInches, Is.EqualTo(HEIGHT_INCHES).Within(0.01d));

					// Test predicate and entity load via HQL
				u = (User)s.CreateQuery("from User u where u.Person.HeightInches between ? and ?")
					.SetDouble(0, HEIGHT_INCHES - 0.01d)
					.SetDouble(1, HEIGHT_INCHES + 0.01d)
					.UniqueResult();
				
				Assert.That(u.Person.HeightInches, Is.EqualTo(HEIGHT_INCHES).Within(0.01d));
				
				// Test update
				u.Person.HeightInches = 1;
				s.Flush();
				heightViaSql = (double)s.CreateSQLQuery("select height_centimeters from t_user where t_user.username='******'").UniqueResult();
				Assert.That(heightViaSql, Is.EqualTo(2.54d).Within(0.01d));
				s.Delete(u);
				t.Commit();
				s.Close();
			}
		}
Exemplo n.º 4
0
		public void TestComponentStateChangeAndDirtiness() 
		{
			// test for HHH-2366
			using (ISession s = sessions.OpenSession())
			using (ITransaction t = s.BeginTransaction())
			{
				User u = new User("steve", "hibernater", new Person( "Steve Ebersole", new DateTime(1999, 12, 31), "Main St"));
				s.Persist(u);
				s.Flush();
				long intialUpdateCount = sessions.Statistics.EntityUpdateCount;
				u.Person.Address = "Austin";
				s.Flush();
				Assert.That(sessions.Statistics.EntityUpdateCount, Is.EqualTo(intialUpdateCount + 1));
				intialUpdateCount = sessions.Statistics.EntityUpdateCount;
				u.Person.Address = "Cedar Park";
				s.Flush();
				Assert.That(sessions.Statistics.EntityUpdateCount, Is.EqualTo(intialUpdateCount + 1));
				s.Delete(u);
				t.Commit();
				s.Close();
			}
		}