Example #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();
			}
		}
Example #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();
			}
		}
Example #4
0
		public void Any()
		{
			ISession s = OpenSession();
			One one = new One();
			BarProxy foo = new Bar();
			foo.Object = one;
			object fid = s.Save(foo);
			object oid = one.Key;
			s.Flush();
			s.Close();

			s = OpenSession();
			IList list = s.CreateQuery("from Bar bar where bar.Object.id = ? and bar.Object.class = ?")
				.SetParameter(0, oid, NHibernateUtil.Int64).SetParameter(1, typeof(One).FullName, NHibernateUtil.ClassMetaType).List();
			Assert.AreEqual(1, list.Count);

			// this is a little different from h2.0.3 because the full type is stored, not
			// just the class name.
			list =
				s.CreateQuery(
					"select one from One one, Bar bar where bar.Object.id = one.id and bar.Object.class LIKE 'NHibernate.DomainModel.One%'")
					.List();
			Assert.AreEqual(1, list.Count);
			s.Flush();
			s.Close();

			s = OpenSession();
			foo = (BarProxy) s.Load(typeof(Foo), fid);
			Assert.IsNotNull(foo);
			Assert.IsTrue(foo.Object is One);
			Assert.AreEqual(oid, s.GetIdentifier(foo.Object));
			s.Delete(foo);
			s.Delete(foo.Object);
			s.Flush();
			s.Close();
		}
Example #5
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();
		}
Example #6
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();
		}
Example #7
0
		public void UpdateCollections()
		{
			ISession s = OpenSession();
			Holder baz = new Holder();
			baz.Name = "123";
			Foo f1 = new Foo();
			Foo f2 = new Foo();
			Foo f3 = new Foo();
			One o = new One();
			baz.Ones = new ArrayList();
			baz.Ones.Add(o);
			Foo[] foos = new Foo[] {f1, null, f2};
			baz.FooArray = foos;
			// in h2.0.3 this is a Set
			baz.Foos = new HashedSet();
			baz.Foos.Add(f1);
			s.Save(f1);
			s.Save(f2);
			s.Save(f3);
			s.Save(o);
			s.Save(baz);
			s.Flush();
			s.Close();

			baz.Ones[0] = null;
			baz.Ones.Add(o);
			baz.Foos.Add(f2);
			foos[0] = f3;
			foos[1] = f1;

			s = OpenSession();
			s.SaveOrUpdate(baz);
			s.Flush();
			s.Close();

			s = OpenSession();
			Holder h = (Holder) s.Load(typeof(Holder), baz.Id);
			Assert.IsNull(h.Ones[0]);
			Assert.IsNotNull(h.Ones[1]);
			Assert.IsNotNull(h.FooArray[0]);
			Assert.IsNotNull(h.FooArray[1]);
			Assert.IsNotNull(h.FooArray[2]);
			Assert.AreEqual(2, h.Foos.Count);

			// new to nh to make sure right items in right index
			Assert.AreEqual(f3.Key, h.FooArray[0].Key);
			Assert.AreEqual(o.Key, ((One) h.Ones[1]).Key);
			Assert.AreEqual(f1.Key, h.FooArray[1].Key);
			Assert.AreEqual(f2.Key, h.FooArray[2].Key);
			s.Close();

			baz.Foos.Remove(f1);
			baz.Foos.Remove(f2);
			baz.FooArray[0] = null;
			baz.FooArray[1] = null;
			baz.FooArray[2] = null;

			s = OpenSession();
			s.SaveOrUpdate(baz);
			s.Delete("from f in class NHibernate.DomainModel.Foo");
			baz.Ones.Remove(o);
			s.Delete("from o in class NHibernate.DomainModel.One");
			s.Delete(baz);
			s.Flush();
			s.Close();
		}
Example #8
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();
		}