Example #1
0
		public Contract(Plan plan, string customerName, string type)
		{
			if (plan != null)
			{
				plans.Add(plan);
				plan.Contracts.Add(this);
			}
			this.customerName = customerName;
			this.type = type;
			variations = new List<ContractVariation>();
			subcontracts = new IESI.HashedSet<Contract>();
			parties = new IESI.HashedSet<Party>();
			infos = new IESI.HashedSet<Info>();
		}
		public void UpdateProperty()
		{
			ClearCounts();
	
			Plan p = new Plan("plan");
			p.AddContract(new Contract(null, "gail", "phone"));
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(p);
			t.Commit();
			s.Close();
	
			AssertInsertCount(2);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			p = s.CreateCriteria<Plan>().UniqueResult<Plan>();
			p.Description = "new plan";
			Assert.That(p.Contracts.Count, Is.EqualTo(1));
			Contract c = p.Contracts.First();
			c.CustomerName = "yogi";
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			p = s.CreateCriteria<Plan>().UniqueResult<Plan>();
			Assert.That(p.Contracts.Count, Is.EqualTo(1));
			c = p.Contracts.First();
			Assert.That(c.CustomerName, Is.EqualTo("gail"));
			if (isPlanContractsBidirectional)
			{
				Assert.That(c.Plans.Count, Is.EqualTo(1));
				Assert.That(c.Plans.First(), Is.SameAs(p));
			}
			s.Delete(p);
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCountInt64()).UniqueResult(), Is.EqualTo(0L));
			Assert.That(s.CreateCriteria<Plan>().SetProjection(Projections.RowCountInt64()).UniqueResult(), Is.EqualTo(0L));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(2);
		}
		public void MoveManyToManyElementToNewEntityCollection()
		{
			ClearCounts();
	
			Plan p = new Plan("plan");
			p.AddContract(new Contract(null, "gail", "phone"));
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(p);
			t.Commit();
			s.Close();
	
			AssertInsertCount(2);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			p = s.CreateCriteria<Plan>().UniqueResult<Plan>();
			Assert.That(p.Contracts.Count, Is.EqualTo(1));
			Contract c = p.Contracts.First();
			Assert.That(c.CustomerName, Is.EqualTo("gail"));
			if (isPlanContractsBidirectional)
			{
				Assert.That(c.Plans.First(), Is.SameAs(p));
			}
			p.RemoveContract(c);
			Plan p2 = new Plan("new plan");
			p2.AddContract(c);
			s.Save(p2);
			t.Commit();
			s.Close();
	
			AssertInsertCount(1);
			AssertUpdateCount(isPlanVersioned && isContractVersioned ? 2 : 0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			p = s.CreateCriteria<Plan>().Add(Restrictions.IdEq(p.Id)).UniqueResult<Plan>();
			p2 = s.CreateCriteria<Plan>().Add(Restrictions.IdEq(p2.Id)).UniqueResult<Plan>();
			/*
			if (isPlanContractsInverse) {
				Assert.That(p.Contracts.Count, Is.EqualTo(1));
				c = p.Contracts.First();
				Assert.That(c.CustomerName, Is.EqualTo("gail"));
				if (isPlanContractsBidirectional) {
					Assert.That(c.Plans.First(), Is.SameAs(p));
				}
				assertEquals( 0, p2.getContracts().size() );
			}
			else {
			*/
				Assert.That(p.Contracts.Count, Is.EqualTo(0));
				Assert.That(p2.Contracts.Count, Is.EqualTo(1));
				c = p2.Contracts.First();
				Assert.That(c.CustomerName, Is.EqualTo("gail"));
				if (isPlanContractsBidirectional)
				{
					Assert.That(c.Plans.First(), Is.SameAs(p2));
				}
			//}
			s.Delete(p);
			s.Delete(p2);
			Assert.That(s.CreateCriteria<Plan>().SetProjection(Projections.RowCountInt64()).UniqueResult(), Is.EqualTo(0L));
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCountInt64()).UniqueResult(), Is.EqualTo(0L));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(3);
		}
		public void ManyToManyCollectionOptimisticLockingWithUpdate()
		{
			ClearCounts();
	
			Plan pOrig = new Plan("plan");
			Contract cOrig = new Contract(null, "gail", "phone");
			pOrig.AddContract(cOrig);
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(pOrig);
			t.Commit();
			s.Close();
	
			AssertInsertCount(2);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			Plan p = s.Get<Plan>(pOrig.Id);
			Contract newC = new Contract(null, "yogi", "pawprint");
			p.AddContract(newC);
			t.Commit();
			s.Close();
	
			AssertInsertCount(1);
			AssertUpdateCount(isContractVersioned ? 1 : 0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			pOrig.RemoveContract(cOrig);
			s.Update(pOrig);
			try
			{
				t.Commit();
				Assert.That(isContractVersioned, Is.False);
			}
			catch (StaleObjectStateException ex)
			{
				Assert.That(isContractVersioned, Is.True);
				t.Rollback();
			}
			s.Close();
	
			s = OpenSession();
			t = s.BeginTransaction();
			p = s.CreateCriteria<Plan>().UniqueResult<Plan>();
			s.Delete(p);
			s.CreateQuery("delete from Contract").ExecuteUpdate();
			Assert.That(s.CreateCriteria<Plan>().SetProjection(Projections.RowCountInt64()).UniqueResult<long>(), Is.EqualTo(0L));
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCountInt64()).UniqueResult<long>(), Is.EqualTo(0L));
			t.Commit();
			s.Close();
		}
		public void RemoveManyToManyElementByDelete()
		{
			ClearCounts();
	
			Plan p = new Plan("plan");
			Contract c = new Contract(null, "gail", "phone");
			p.AddContract(c);
	
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(p);
			t.Commit();
			s.Close();
	
			AssertInsertCount(2);
			AssertUpdateCount(0);
			ClearCounts();
	
			p.RemoveContract(c);
			Assert.That(p.Contracts.Count, Is.EqualTo(0));
			if (isPlanContractsBidirectional)
			{
				Assert.That(c.Plans.Count, Is.EqualTo(0));
			}
	
			s = OpenSession();
			t = s.BeginTransaction();
			s.Update(p);
			s.Delete(c);
			t.Commit();
			s.Close();
	
			AssertUpdateCount(isPlanVersioned ? 1 : 0);
			AssertDeleteCount(1);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			p = s.CreateCriteria<Plan>().UniqueResult<Plan>();
			Assert.That(p.Contracts.Count, Is.EqualTo(0));
			s.Delete(p);
			Assert.That(s.CreateCriteria<Plan>().SetProjection(Projections.RowCountInt64()).UniqueResult<long>(), Is.EqualTo(0L));
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCountInt64()).UniqueResult<long>(), Is.EqualTo(0L));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(1);
		}
		public void CreateWithNonEmptyManyToManyCollectionMergeWithNewElement()
		{
			ClearCounts();
	
			Plan p = new Plan("plan");
			Contract c = new Contract(null, "gail", "phone");
			p.AddContract(c);
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(p);
			t.Commit();
			s.Close();
	
			AssertInsertCount(2);
			AssertUpdateCount(0);
			ClearCounts();
	
			Contract newC = new Contract(null, "yogi", "mail");
			p.AddContract(newC);
	
			s = OpenSession();
			t = s.BeginTransaction();
			p = (Plan)s.Merge(p);
			t.Commit();
			s.Close();
	
			AssertInsertCount(1);
			AssertUpdateCount(isContractVersioned && isPlanVersioned ? 1 : 0);  // NH-specific: Hibernate issues a seperate UPDATE for the version number
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			p = s.CreateCriteria<Plan>().UniqueResult<Plan>();
			Assert.That(p.Contracts.Count, Is.EqualTo(2));
			foreach (Contract aContract in p.Contracts)
			{
				if (aContract.Id == c.Id)
				{
					Assert.That(aContract.CustomerName, Is.EqualTo("gail"));
				}
				else if (!aContract.CustomerName.Equals(newC.CustomerName))
				{
					Assert.Fail("unknown contract:" + aContract.CustomerName);
				}
				if (isPlanContractsBidirectional)
				{
					Assert.That(aContract.Plans.First(), Is.SameAs(p));
				}
			}
			s.Delete(p);
			Assert.That(s.CreateCriteria<Plan>().SetProjection(Projections.RowCountInt64()).UniqueResult(), Is.EqualTo(0L));
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCountInt64()).UniqueResult(), Is.EqualTo(0L));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(3);
		}
		public void CreateWithEmptyManyToManyCollectionMergeWithExistingElement()
		{
			ClearCounts();
	
			Plan p = new Plan("plan");
			Contract c = new Contract(null, "gail", "phone");
	
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(p);
			s.Persist(c);
			t.Commit();
			s.Close();
	
			AssertInsertCount(2);
			AssertUpdateCount(0);
			ClearCounts();
	
			p.AddContract(c);
	
			s = OpenSession();
			t = s.BeginTransaction();
			p = (Plan)s.Merge(p);
			t.Commit();
			s.Close();
	
			AssertInsertCount(0);
			AssertUpdateCount(isContractVersioned && isPlanVersioned ? 2 : 0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			p = s.CreateCriteria<Plan>().UniqueResult<Plan>();
			Assert.That(p.Contracts.Count, Is.EqualTo(1));
			c = p.Contracts.First();
			Assert.That(c.CustomerName, Is.EqualTo("gail"));
			if (isPlanContractsBidirectional)
			{
				Assert.That(c.Plans.First(), Is.SameAs(p));
			}
			s.Delete(p);
			Assert.That(s.CreateCriteria<Plan>().SetProjection(Projections.RowCountInt64()).UniqueResult<long>(), Is.EqualTo(0L));
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCountInt64()).UniqueResult<long>(), Is.EqualTo(0L));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(2);
		}
		public void CreateWithNonEmptyManyToManyCollectionUpdateWithNewElement()
		{
			ClearCounts();
	
			Plan p = new Plan("plan");
			Contract c = new Contract(null, "gail", "phone");
			p.AddContract(c);
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(p);
			t.Commit();
			s.Close();
	
			AssertInsertCount(2);
			AssertUpdateCount(0);
			ClearCounts();
	
			Contract newC = new Contract(null, "sherman", "telepathy");
			p.AddContract(newC);
	
			s = OpenSession();
			t = s.BeginTransaction();
			s.Update(p);
			t.Commit();
			s.Close();
	
			AssertInsertCount(1);
			AssertUpdateCount(isContractVersioned ? 1 : 0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			p = s.CreateCriteria<Plan>().UniqueResult<Plan>();
			Assert.That(p.Contracts.Count, Is.EqualTo(2));
			foreach (Contract aContract in p.Contracts)
			{
				if (aContract.Id == c.Id)
				{
					Assert.That(aContract.CustomerName, Is.EqualTo("gail"));
				}
				else if (aContract.Id == newC.Id)
				{
					Assert.That(aContract.CustomerName, Is.EqualTo("sherman"));
				}
				else
				{
					Assert.Fail("unknown contract");
				}
				if (isPlanContractsBidirectional)
				{
					Assert.That(aContract.Plans.First(), Is.SameAs(p));
				}
			}
			s.Delete(p);
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCountInt64()).UniqueResult<long>(), Is.EqualTo(0L));
			Assert.That(s.CreateCriteria<Plan>().SetProjection(Projections.RowCountInt64()).UniqueResult<long>(), Is.EqualTo(0L));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(3);
		}
		public void AddNewManyToManyElementToPersistentEntity()
		{
			ClearCounts();
	
			Plan p = new Plan("plan");
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			s.Persist(p);
			t.Commit();
			s.Close();
	
			AssertInsertCount(1);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			p = s.Get<Plan>(p.Id);
			Assert.That(p.Contracts.Count, Is.EqualTo(0));
			p.AddContract(new Contract(null, "gail", "phone"));
			t.Commit();
			s.Close();
	
			AssertInsertCount(1);
			AssertUpdateCount(isContractVersioned ? 1 : 0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			p = s.CreateCriteria<Plan>().UniqueResult<Plan>();
			Assert.That(p.Contracts.Count, Is.EqualTo(1));
			Contract c = p.Contracts.First();
			Assert.That(c.CustomerName, Is.EqualTo("gail"));
			if (isPlanContractsBidirectional)
			{
				Assert.That(c.Plans.Count, Is.EqualTo(1));
				Assert.That(c.Plans.First(), Is.SameAs(p));
			}
			s.Delete(p);
			Assert.That(s.CreateCriteria<Contract>().SetProjection(Projections.RowCountInt64()).UniqueResult(), Is.EqualTo(0L));
			Assert.That(s.CreateCriteria<Plan>().SetProjection(Projections.RowCountInt64()).UniqueResult(), Is.EqualTo(0L));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(2);
		}