Пример #1
0
		public void CollectionVersioning()
		{
			using (ISession s = OpenSession())
			{
				One one = new One();
				one.Manies = new HashedSet();
				s.Save(one);
				s.Flush();

				Many many = new Many();
				many.One = one;
				one.Manies.Add(many);
				s.Save(many);
				s.Flush();

				// Versions are incremented compared to Hibernate because they start from 1
				// in NH.
				Assert.AreEqual(1, many.V);
				Assert.AreEqual(2, one.V);

				s.Delete(many);
				s.Delete(one);
				s.Flush();
			}
		}
Пример #2
0
		protected override void OnSetUp()
		{
			base.OnSetUp();

			// create the objects to search on		
			one = new One();
			one.X = 20;
			one.Manies = new HashSet<Many>();

			Many many1 = new Many();
			many1.X = 10;
			many1.One = one;
			one.Manies.Add( many1 );

			Many many2 = new Many();
			many2.X = 20;
			many2.One = one;
			one.Manies.Add( many2 );

			using( ISession s = OpenSession() )
			using( ITransaction t = s.BeginTransaction() )
			{
				s.Save( one );
				s.Save( many1 );
				s.Save( many2 );
				t.Commit();
			}
		}
		public void CollectionVersioning()
		{
			using( ISession s = OpenSession() )
			{
				One one = new One();
				one.Manies = new HashedSet();
				s.Save( one );
				s.Flush();

				Many many = new Many();
				many.One = one;
				one.Manies.Add( many );
				s.Save( many );
				s.Flush();

				Assert.AreEqual( 0, many.V );
				Assert.AreEqual( 1, one.V );

				s.Delete( many );
				s.Delete( one );
				s.Flush();
			}
		}
Пример #4
0
		public void ManyToOne()
		{
			ISession s = OpenSession();
			One one = new One();
			s.Save(one);
			one.Value = "yada";
			Many many = new Many();
			many.One = one;
			s.Save(many);
			s.Flush();
			s.Close();

			s = OpenSession();
			one = (One) s.Load(typeof(One), one.Key);
			int countManies = one.Manies.Count;
			s.Close();

			s = OpenSession();
			many = (Many) s.Load(typeof(Many), many.Key);
			Assert.IsNotNull(many.One, "many-to-one assoc");
			s.Delete(many.One);
			s.Delete(many);
			s.Flush();
			s.Close();
		}
Пример #5
0
		public void OrderBy()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Foo foo = new Foo();
			s.Save(foo);
			IList list =
				s.CreateQuery(
					"select foo from foo in class Foo, fee in class Fee where foo.Dependent = fee order by foo.String desc, foo.Component.Count asc, fee.id")
					.List();
			Assert.AreEqual(1, list.Count, "order by");
			Foo foo2 = new Foo();
			s.Save(foo2);
			foo.TheFoo = foo2;
			list =
				s.CreateQuery(
					"select foo.TheFoo, foo.Dependent from foo in class Foo order by foo.TheFoo.String desc, foo.Component.Count asc, foo.Dependent.id")
					.List();
			Assert.AreEqual(1, list.Count, "order by");
			list =
				s.CreateQuery("select foo from foo in class NHibernate.DomainModel.Foo order by foo.Dependent.id, foo.Dependent.Fi")
					.List();
			Assert.AreEqual(2, list.Count, "order by");
			s.Delete(foo);
			s.Delete(foo2);
			t.Commit();
			s.Close();

			s = OpenSession();
			Many manyB = new Many();
			s.Save(manyB);
			One oneB = new One();
			s.Save(oneB);
			oneB.Value = "b";
			manyB.One = oneB;
			Many manyA = new Many();
			s.Save(manyA);
			One oneA = new One();
			s.Save(oneA);
			oneA.Value = "a";
			manyA.One = oneA;
			s.Flush();
			s.Close();

			s = OpenSession();
			IEnumerable enumerable =
				s.CreateQuery("SELECT one FROM one IN CLASS " + typeof(One).Name + " ORDER BY one.Value ASC").Enumerable();
			int count = 0;
			foreach (One one in enumerable)
			{
				switch (count)
				{
					case 0:
						Assert.AreEqual("a", one.Value, "a - ordering failed");
						break;
					case 1:
						Assert.AreEqual("b", one.Value, "b - ordering failed");
						break;
					default:
						Assert.Fail("more than two elements");
						break;
				}
				count++;
			}

			s.Flush();
			s.Close();

			s = OpenSession();
			enumerable =
				s.CreateQuery("SELECT many.One FROM many IN CLASS " + typeof(Many).Name +
				              " ORDER BY many.One.Value ASC, many.One.id").Enumerable();
			count = 0;
			foreach (One one in enumerable)
			{
				switch (count)
				{
					case 0:
						Assert.AreEqual("a", one.Value, "'a' should be first element");
						break;
					case 1:
						Assert.AreEqual("b", one.Value, "'b' should be second element");
						break;
					default:
						Assert.Fail("more than 2 elements");
						break;
				}
				count++;
			}
			s.Flush();
			s.Close();

			s = OpenSession();
			oneA = (One) s.Load(typeof(One), oneA.Key);
			manyA = (Many) s.Load(typeof(Many), manyA.Key);
			oneB = (One) s.Load(typeof(One), oneB.Key);
			manyB = (Many) s.Load(typeof(Many), manyB.Key);
			s.Delete(manyA);
			s.Delete(oneA);
			s.Delete(manyB);
			s.Delete(oneB);
			s.Flush();
			s.Close();
		}
Пример #6
0
		public void Container()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Container c = new Container();
			Simple x = new Simple();
			x.Count = 123;
			Simple y = new Simple();
			y.Count = 456;
			s.Save(x, (long) 1);
			s.Save(y, (long) 0);
			IList o2m = new ArrayList();
			o2m.Add(x);
			o2m.Add(null);
			o2m.Add(y);
			IList m2m = new ArrayList();
			m2m.Add(x);
			m2m.Add(null);
			m2m.Add(y);
			c.OneToMany = o2m;
			c.ManyToMany = m2m;
			IList comps = new ArrayList();
			Container.ContainerInnerClass ccic = new Container.ContainerInnerClass();
			ccic.Name = "foo";
			ccic.Simple = x;
			comps.Add(ccic);
			comps.Add(null);
			ccic = new Container.ContainerInnerClass();
			ccic.Name = "bar";
			ccic.Simple = y;
			comps.Add(ccic);

			ISet compos = new HashedSet();
			compos.Add(ccic);
			c.Composites = compos;
			c.Components = comps;
			One one = new One();
			Many many = new Many();
			ISet manies = new HashedSet();
			manies.Add(many);
			one.Manies = manies;
			many.One = one;
			ccic.Many = many;
			ccic.One = one;
			s.Save(one);
			s.Save(many);
			s.Save(c);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.Load(typeof(Container), c.Id);

			ccic = (Container.ContainerInnerClass) c.Components[2];
			Assert.AreEqual(ccic.One, ccic.Many.One);
			Assert.AreEqual(3, c.Components.Count);
			Assert.AreEqual(1, c.Composites.Count);
			Assert.AreEqual(3, c.OneToMany.Count);
			Assert.AreEqual(3, c.ManyToMany.Count);

			for (int i = 0; i < 3; i++)
			{
				Assert.AreEqual(c.ManyToMany[i], c.OneToMany[i]);
			}
			object o1 = c.OneToMany[0];
			object o2 = c.OneToMany[2];
			c.OneToMany.RemoveAt(2);
			c.OneToMany[0] = o2;
			c.OneToMany[1] = o1;
			o1 = c.Components[2];
			c.Components.RemoveAt(2);
			c.Components[0] = o1;
			c.ManyToMany[0] = c.ManyToMany[2];
			c.Composites.Add(o1);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.Load(typeof(Container), c.Id);
			Assert.AreEqual(1, c.Components.Count); //WAS: 2 - h2.0.3 comment
			Assert.AreEqual(2, c.Composites.Count);
			Assert.AreEqual(2, c.OneToMany.Count);
			Assert.AreEqual(3, c.ManyToMany.Count);
			Assert.IsNotNull(c.OneToMany[0]);
			Assert.IsNotNull(c.OneToMany[1]);

			((Container.ContainerInnerClass) c.Components[0]).Name = "a different name";
			IEnumerator enumer = c.Composites.GetEnumerator();
			enumer.MoveNext();
			((Container.ContainerInnerClass) enumer.Current).Name = "once again";
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.Load(typeof(Container), c.Id);
			Assert.AreEqual(1, c.Components.Count); //WAS: 2 -> h2.0.3 comment
			Assert.AreEqual(2, c.Composites.Count);
			Assert.AreEqual("a different name", ((Container.ContainerInnerClass) c.Components[0]).Name);
			enumer = c.Composites.GetEnumerator();
			bool found = false;
			while (enumer.MoveNext())
			{
				if (((Container.ContainerInnerClass) enumer.Current).Name.Equals("once again"))
				{
					found = true;
				}
			}

			Assert.IsTrue(found);
			c.OneToMany.Clear();
			c.ManyToMany.Clear();
			c.Composites.Clear();
			c.Components.Clear();
			s.Delete("from s in class Simple");
			s.Delete("from m in class Many");
			s.Delete("from o in class One");
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.Load(typeof(Container), c.Id);
			Assert.AreEqual(0, c.Components.Count);
			Assert.AreEqual(0, c.Composites.Count);
			Assert.AreEqual(0, c.OneToMany.Count);
			Assert.AreEqual(0, c.ManyToMany.Count);
			s.Delete(c);
			t.Commit();
			s.Close();
		}