public void ImmutableCollectionWithUpdate()
		{
			ClearCounts();
	
			Contract c = new Contract(null, "gavin", "phone");
			ContractVariation cv1 = new ContractVariation(1, c);
			cv1.Text = "expensive";
			ContractVariation cv2 = new ContractVariation(2, c);
			cv2.Text = "more expensive";
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(c);
			t.Commit();
			s.Close();
	
			AssertInsertCount(3);
			AssertUpdateCount(0);
	
			s = OpenSession();
			t = s.BeginTransaction();
			c.Variations.Add(new ContractVariation(3, c));
			s.Update(c);
			try
			{
				t.Commit();
				Assert.Fail( "should have failed because reassociated object has a dirty collection");
			}
			catch (HibernateException ex)
			{
				// expected
			}
			finally
			{
				t.Rollback();
				s.Close();
			}
	
			AssertUpdateCount(0);
	
			s = OpenSession();
			t = s.BeginTransaction();
			c = s.CreateCriteria<Contract>().UniqueResult<Contract>();
			Assert.That(c.CustomerName, Is.EqualTo("gavin"));
			Assert.That(c.Variations.Count, Is.EqualTo(2));
			IEnumerator<ContractVariation> it = c.Variations.GetEnumerator();
			it.MoveNext();
			cv1 = it.Current;
			Assert.That(cv1.Text, Is.EqualTo("expensive"));
			it.MoveNext();
			cv2 = it.Current;
			Assert.That(cv2.Text, Is.EqualTo("more expensive"));
			s.Delete(c);
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			Assert.That(s.CreateCriteria<ContractVariation>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(3);
		}
		public void ImmutableParentEntityWithMerge()
		{
			ClearCounts();
	
			Contract c = new Contract(null, "gavin", "phone");
			ContractVariation cv1 = new ContractVariation(1, c);
			cv1.Text = "expensive";
			ContractVariation cv2 = new ContractVariation(2, c);
			cv2.Text = "more expensive";
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(c);
			t.Commit();
			s.Close();
	
			AssertInsertCount(3);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			c.CustomerName = "foo bar";
			c = (Contract)s.Merge(c);
			Assert.That(s.IsReadOnly(c), Is.True);
			Assert.That(NHibernateUtil.IsInitialized(c.Variations), Is.True);
			IEnumerator<ContractVariation> it = c.Variations.GetEnumerator();
			it.MoveNext();
			cv1 = it.Current;
			it.MoveNext();
			cv2 = it.Current;
			Assert.That(s.IsReadOnly(c), Is.True);
			Assert.That(s.IsReadOnly(c), Is.True);
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
	
			s = OpenSession();
			t = s.BeginTransaction();
			c = s.CreateCriteria<Contract>().UniqueResult<Contract>();
			Assert.That(c.CustomerName, Is.EqualTo("gavin"));
			Assert.That(c.Variations.Count, Is.EqualTo(2));
			it = c.Variations.GetEnumerator();
			it.MoveNext();
			cv1 = it.Current;
			Assert.That(cv1.Text, Is.EqualTo("expensive"));
			it.MoveNext();
			cv2 = it.Current;
			Assert.That(cv2.Text, Is.EqualTo("more expensive"));
			s.Delete(c);
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			Assert.That(s.CreateCriteria<ContractVariation>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(3);
	
		}
		public void DeleteDetachedImmutable()
		{
			ClearCounts();
	
			Contract c = new Contract(null, "gavin", "phone");
			ContractVariation cv1 = new ContractVariation(1, c);
			cv1.Text = "expensive";
			ContractVariation cv2 = new ContractVariation(2, c);
			cv2.Text = "more expensive";
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(c);
			t.Commit();
			s.Close();
	
			AssertInsertCount(3);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			s.Delete(c);
			c = s.CreateCriteria<Contract>().UniqueResult<Contract>();
			Assert.That(c, Is.Null);
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(3);
		}
		public void DeleteDetachedModifiedImmutable()
		{
			ClearCounts();
	
			Contract c = new Contract(null, "gavin", "phone");
			ContractVariation cv1 = new ContractVariation(1, c);
			cv1.Text = "expensive";
			ContractVariation cv2 = new ContractVariation(2, c);
			cv2.Text = "more expensive";
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(c);
			t.Commit();
			s.Close();
	
			AssertInsertCount(3);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			c.CustomerName = "Sherman";
			s.Delete(c);
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(3);
		}
		public void GetAndDeleteManagedImmutable()
		{
			ClearCounts();
	
			Contract c = new Contract(null, "gavin", "phone");
			ContractVariation cv1 = new ContractVariation(1, c);
			cv1.Text = "expensive";
			ContractVariation cv2 = new ContractVariation(2, c);
			cv2.Text = "more expensive";
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(c);
			t.Commit();
			s.Close();
	
			AssertInsertCount(3);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			c = s.Get<Contract>(c.Id);
			Assert.That(s.IsReadOnly(c), Is.True);
			Assert.That(c.CustomerName, Is.EqualTo("gavin"));
			Assert.That(c.Variations.Count, Is.EqualTo(2));
			IEnumerator<ContractVariation> it = c.Variations.GetEnumerator();
			it.MoveNext();
			cv1 = it.Current;
			Assert.That(cv1.Text, Is.EqualTo("expensive"));
			it.MoveNext();
			cv2 = it.Current;
			Assert.That(cv2.Text, Is.EqualTo("more expensive"));
			Assert.That(s.IsReadOnly(cv1), Is.True);
			Assert.That(s.IsReadOnly(cv2), Is.True);
			c.CustomerName = "Sherman";
			s.Delete(c);
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			Assert.That(s.CreateCriteria<ContractVariation>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(3);
		}
		public void ChangeImmutableEntityProxyToModifiable()
		{
			Contract c = new Contract(null, "gavin", "phone");
			ContractVariation cv1 = new ContractVariation(1, c);
			cv1.Text = "expensive";
			ContractVariation cv2 = new ContractVariation(2, c);
			cv2.Text = "more expensive";
	
			ClearCounts();
	
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(c);
			Assert.That(s.IsReadOnly(c), Is.True);
			Assert.That(s.IsReadOnly(cv1), Is.True);
			Assert.That(s.IsReadOnly(cv2), Is.True);
			t.Commit();
			s.Close();
	
			AssertInsertCount(3);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			c = s.CreateCriteria<Contract>().UniqueResult<Contract>();
			Assert.That(s.IsReadOnly(c), Is.True);
			Assert.That(c.CustomerName, Is.EqualTo("gavin"));
			Assert.That(c.Variations.Count, Is.EqualTo(2));
			IEnumerator<ContractVariation> it = c.Variations.GetEnumerator();
			it.MoveNext();
			cv1 = it.Current;
			Assert.That(cv1.Text, Is.EqualTo("expensive"));
			it.MoveNext();
			cv2 = it.Current;
			Assert.That(cv2.Text, Is.EqualTo("more expensive"));
			Assert.That(s.IsReadOnly(cv1), Is.True);
			Assert.That(s.IsReadOnly(cv2), Is.True);
	
			try
			{
				Assert.That(c, Is.InstanceOf<INHibernateProxy>());
				s.SetReadOnly(c, false);
			}
			catch (System.InvalidOperationException ex)
			{
				// expected
			}
			finally
			{
				t.Rollback();
				s.Close();
			}
	
			s = OpenSession();
			t = s.BeginTransaction();
			s.Delete(c);
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			Assert.That(s.CreateCriteria<ContractVariation>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(3);
		}
		public void ImmutableEntityRemoveImmutableFromInverseMutableCollectionByDeref()
		{
			ClearCounts();
	
			Contract c = new Contract(null, "gavin", "phone");
			ContractVariation cv1 = new ContractVariation(1, c);
			cv1.Text = "expensive";
			ContractVariation cv2 = new ContractVariation(2, c);
			cv2.Text = "more expensive";
			Party party = new Party( "party1" );
			c.AddParty(party);
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(c);
			t.Commit();
			s.Close();
	
			AssertInsertCount(4);
			AssertUpdateCount(0);
			ClearCounts();
	
			party = c.Parties.First();
			party.Contract = null;
	
			s = OpenSession();
			t = s.BeginTransaction();
			s.Update(party);
			t.Commit();
			s.Close();
	
			s = OpenSession();
			t = s.BeginTransaction();
			party = s.Get<Party>(party.Id);
			Assert.That(party.Contract, Is.Not.Null);
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			c = s.CreateCriteria<Contract>().UniqueResult<Contract>();
			Assert.That(c.CustomerName, Is.EqualTo("gavin"));
			Assert.That(c.Variations.Count, Is.EqualTo(2));
			IEnumerator<ContractVariation> it = c.Variations.GetEnumerator();
			it.MoveNext();
			cv1 = it.Current;
			Assert.That(cv1.Text, Is.EqualTo("expensive"));
			it.MoveNext();
			cv2 = it.Current;
			Assert.That(cv2.Text, Is.EqualTo("more expensive"));
			Assert.That(c.Parties.Count, Is.EqualTo(1));
		    party = c.Parties.First();
			Assert.That(party.Name, Is.EqualTo("party1"));
			Assert.That(party.Contract, Is.SameAs(c));
			s.Delete(c);
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			Assert.That(s.CreateCriteria<ContractVariation>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(4);
		}
		public void ImmutableEntityAddImmutableToInverseMutableCollection()
		{
			ClearCounts();
	
			Contract c = new Contract(null, "gavin", "phone");
			ContractVariation cv1 = new ContractVariation(1, c);
			cv1.Text = "expensive";
			ContractVariation cv2 = new ContractVariation(2, c);
			cv2.Text = "more expensive";
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(c);
			Party party = new Party("a party");
			s.Persist(party);
			t.Commit();
			s.Close();
	
			AssertInsertCount(4);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			c.AddParty(new Party("a new party"));
			s.Update(c);
			t.Commit();
			s.Close();
	
			AssertInsertCount(1);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			c.AddParty(party);
			s.Update(c);
			t.Commit();
			s.Close();
	
			s = OpenSession();
			t = s.BeginTransaction();
			c = s.CreateCriteria<Contract>().UniqueResult<Contract>();
			Assert.That(c.CustomerName, Is.EqualTo("gavin"));
			Assert.That(c.Variations.Count, Is.EqualTo(2));
			IEnumerator<ContractVariation> it = c.Variations.GetEnumerator();
			it.MoveNext();
			cv1 = it.Current;
			Assert.That(cv1.Text, Is.EqualTo("expensive"));
			it.MoveNext();
			cv2 = it.Current;
			Assert.That(cv2.Text, Is.EqualTo("more expensive"));
			// Assert.That(c.Parties.Count, Is.EqualTo(2));
			s.Delete(c);
			s.Delete(party); // NH-specific
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			Assert.That(s.CreateCriteria<ContractVariation>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(5); // NH-specific
		}
		public void UpdatedEntityViaImmutableEntityWithImmutableCollectionUsingMerge()
		{
			ClearCounts();
	
			Contract c = new Contract(null, "gavin", "phone");
			ContractVariation cv1 = new ContractVariation(1, c);
			cv1.Text = "expensive";
			Info cv1Info = new Info( "cv1 info" );
			cv1.Infos.Add(cv1Info);
			ContractVariation cv2 = new ContractVariation(2, c);
			cv2.Text = "more expensive";
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(c);
			t.Commit();
			s.Close();
	
			AssertInsertCount(4);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			cv1Info.Text = "new cv1 info";
			s.Merge(c);
			t.Commit();
			s.Close();
	
			AssertInsertCount(0);
			AssertUpdateCount(1);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			c = s.CreateCriteria<Contract>().UniqueResult<Contract>();
			Assert.That(c.CustomerName, Is.EqualTo("gavin"));
			Assert.That(c.Variations.Count, Is.EqualTo(2));
			IEnumerator<ContractVariation> it = c.Variations.GetEnumerator();
			it.MoveNext();
			cv1 = it.Current;
			Assert.That(cv1.Text, Is.EqualTo("expensive"));
			Assert.That(cv1.Infos.Count, Is.EqualTo(1));
			Assert.That(cv1.Infos.First().Text, Is.EqualTo("new cv1 info"));
			it.MoveNext();
			cv2 = it.Current;
			Assert.That(cv2.Text, Is.EqualTo("more expensive"));
			s.Delete(c);
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			Assert.That(s.CreateCriteria<ContractVariation>().SetProjection(Projections.RowCount()).UniqueResult(), Is.EqualTo(0));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(4);
		}