예제 #1
0
		public void Replicate()
		{
			ISession s = OpenSession();
			Container baz = new Container();
			Contained f = new Contained();
			IList list = new ArrayList();
			list.Add(baz);
			f.Bag = list;
			IList list2 = new ArrayList();
			list2.Add(f);
			baz.Bag = list2;
			s.Save(f);
			s.Save(baz);
			s.Flush();
			s.Close();

			s = OpenSession();
			s.Replicate(baz, ReplicationMode.Overwrite);
			s.Flush();
			s.Close();
			s = OpenSession();
			s.Replicate(baz, ReplicationMode.Ignore);
			s.Flush();
			s.Close();

			s = OpenSession();
			s.Delete(baz);
			s.Delete(f);
			s.Flush();
			s.Close();
		}
예제 #2
0
		public void Bag()
		{
			//if( dialect is Dialect.HSQLDialect ) return;

			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Container c = new Container();
			Contained c1 = new Contained();
			Contained c2 = new Contained();
			c.Bag = new ArrayList();
			c.Bag.Add(c1);
			c.Bag.Add(c2);
			c1.Bag.Add(c);
			c2.Bag.Add(c);
			s.Save(c);
			c.Bag.Add(c2);
			c2.Bag.Add(c);
			c.LazyBag.Add(c1);
			c1.LazyBag.Add(c);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.CreateQuery("from c in class ContainerX").List()[0];
			Assert.AreEqual(1, c.LazyBag.Count);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.CreateQuery("from c in class ContainerX").List()[0];
			Contained c3 = new Contained();
			// commented out in h2.0.3 also
			//c.Bag.Add(c3);
			//c3.Bag.Add(c);
			c.LazyBag.Add(c3);
			c3.LazyBag.Add(c);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.CreateQuery("from c in class ContainerX").List()[0];
			Contained c4 = new Contained();
			c.LazyBag.Add(c4);
			c4.LazyBag.Add(c);
			Assert.AreEqual(3, c.LazyBag.Count); //forces initialization
			// s.Save(c4); commented in h2.0.3 also
			t.Commit();
			s.Close();

			// new test code in here to catch when a lazy bag has an addition then
			// is flushed and then an operation that causes it to read from the db
			// occurs - this used to cause the element in Add(element) to be in there
			// twice and throw off the count by 1 (or by however many additions were 
			// made before the flush
			s = OpenSession();
			c = (Container) s.CreateQuery("from c in class ContainerX").List()[0];
			Contained c5 = new Contained();
			c.LazyBag.Add(c5);
			c5.LazyBag.Add(c);

			// this causes the additions to be written to the db - now verify
			// the additions list was cleared and the count returns correctly
			s.Flush();
			Assert.AreEqual(4, c.LazyBag.Count, "correct additions clearing after flush");
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.CreateQuery("from c in class ContainerX").List()[0];
			int j = 0;
			foreach (object obj in c.Bag)
			{
				Assert.IsNotNull(obj);
				j++;
			}

			Assert.AreEqual(3, j);

			// this used to be 3 - but since I added an item in the test above for flush
			// I increased it to 4
			Assert.AreEqual(4, c.LazyBag.Count);
			s.Delete(c);
			c.Bag.Remove(c2);

			j = 0;
			foreach (object obj in c.Bag)
			{
				j++;
				s.Delete(obj);
			}

			Assert.AreEqual(2, j);
			s.Delete(s.Load(typeof(Contained), c5.Id));
			s.Delete(s.Load(typeof(Contained), c4.Id));
			s.Delete(s.Load(typeof(Contained), c3.Id));
			t.Commit();
			s.Close();
		}
예제 #3
0
		public void CascadeCompositeElements()
		{
			Container c = new Container();
			IList list = new ArrayList();
			c.Cascades = list;
			Container.ContainerInnerClass cic = new Container.ContainerInnerClass();
			cic.Many = new Many();
			cic.One = new One();
			list.Add(cic);
			ISession s = OpenSession();
			s.Save(c);
			s.Flush();
			s.Close();

			s = OpenSession();
			foreach (Container obj in s.CreateQuery("from c in class ContainerX").Enumerable())
			{
				c = obj;
				break;
			}
			foreach (Container.ContainerInnerClass obj in c.Cascades)
			{
				cic = obj;
				break;
			}
			Assert.IsNotNull(cic.Many);
			Assert.IsNotNull(cic.One);
			Assert.AreEqual(1, c.Cascades.Count);
			s.Delete(c);
			s.Flush();
			s.Close();

			c = new Container();
			s = OpenSession();
			s.Save(c);
			list = new ArrayList();
			c.Cascades = list;
			cic = new Container.ContainerInnerClass();
			cic.Many = new Many();
			cic.One = new One();
			list.Add(cic);
			s.Flush();
			s.Close();

			s = OpenSession();
			foreach (Container obj in s.CreateQuery("from c in class ContainerX").Enumerable())
			{
				c = obj;
			}
			foreach (Container.ContainerInnerClass obj in c.Cascades)
			{
				cic = obj;
			}
			Assert.IsNotNull(cic.Many);
			Assert.IsNotNull(cic.One);
			Assert.AreEqual(1, c.Cascades.Count);
			s.Delete(c);
			s.Flush();
			s.Close();
		}
예제 #4
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();
		}
예제 #5
0
		public void ManyToMany()
		{
			// if( dialect is Dialect.HSQLDialect) return;

			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Container c = new Container();
			c.ManyToMany = new ArrayList();
			c.Bag = new ArrayList();
			Simple s1 = new Simple();
			Simple s2 = new Simple();
			s1.Count = 123;
			s2.Count = 654;
			Contained c1 = new Contained();
			c1.Bag = new ArrayList();
			c1.Bag.Add(c);
			c.Bag.Add(c1);
			c.ManyToMany.Add(s1);
			c.ManyToMany.Add(s2);
			object cid = s.Save(c);
			s.Save(s1, (long) 12);
			s.Save(s2, (long) -1);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.Load(typeof(Container), cid);
			Assert.AreEqual(1, c.Bag.Count);
			Assert.AreEqual(2, c.ManyToMany.Count);
			foreach (object obj in c.Bag)
			{
				c1 = (Contained) obj;
				break;
			}
			c.Bag.Remove(c1);
			c1.Bag.Remove(c);
			Assert.IsNotNull(c.ManyToMany[0]);
			c.ManyToMany.RemoveAt(0);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			c = (Container) s.Load(typeof(Container), cid);
			Assert.AreEqual(0, c.Bag.Count);
			Assert.AreEqual(1, c.ManyToMany.Count);
			c1 = (Contained) s.Load(typeof(Contained), c1.Id);
			Assert.AreEqual(0, c1.Bag.Count);
			Assert.AreEqual(1, s.Delete("from c in class ContainerX"));
			Assert.AreEqual(1, s.Delete("from c in class Contained"));
			Assert.AreEqual(2, s.Delete("from s in class Simple"));
			t.Commit();
			s.Close();
		}
예제 #6
0
		public void CollectionQuery()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();

			Simple s1 = new Simple();
			s1.Name = "s";
			s1.Count = 0;
			Simple s2 = new Simple();
			s2.Count = 2;
			Simple s3 = new Simple();
			s3.Count = 3;
			s.Save(s1, (long) 1);
			s.Save(s2, (long) 2);
			s.Save(s3, (long) 3);
			Container c = new Container();
			IList l = new ArrayList();
			l.Add(s1);
			l.Add(s3);
			l.Add(s2);
			c.OneToMany = l;
			l = new ArrayList();
			l.Add(s1);
			l.Add(null);
			l.Add(s2);
			c.ManyToMany = l;
			s.Save(c);

			Assert.AreEqual(1,
			                s.CreateQuery("select c from c in class ContainerX, s in class Simple where c.OneToMany[2] = s").List
			                	().Count);
			Assert.AreEqual(1,
			                s.CreateQuery("select c from c in class ContainerX, s in class Simple where c.ManyToMany[2] = s").
			                	List().Count);
			Assert.AreEqual(1,
			                s.CreateQuery("select c from c in class ContainerX, s in class Simple where s = c.OneToMany[2]").List
			                	().Count);
			Assert.AreEqual(1,
			                s.CreateQuery("select c from c in class ContainerX, s in class Simple where s = c.ManyToMany[2]").
			                	List().Count);
			Assert.AreEqual(1, s.CreateQuery("select c from c in class ContainerX where c.OneToMany[0].Name = 's'").List().Count);
			Assert.AreEqual(1, s.CreateQuery("select c from c in class ContainerX where c.ManyToMany[0].Name = 's'").List().Count);
			Assert.AreEqual(1,
			                s.CreateQuery("select c from c in class ContainerX where 's' = c.OneToMany[2 - 2].Name").List().Count);
			Assert.AreEqual(1,
			                s.CreateQuery("select c from c in class ContainerX where 's' = c.ManyToMany[(3+1)/4-1].Name").List().
			                	Count);
			if (Dialect.SupportsSubSelects)
			{
				Assert.AreEqual(1,
				                s.CreateQuery(
				                	"select c from c in class ContainerX where c.ManyToMany[ c.ManyToMany.maxIndex ].Count = 2").List()
				                	.Count);
				Assert.AreEqual(1,
				                s.CreateQuery(
				                	"select c from c in class ContainerX where c.ManyToMany[ maxindex(c.ManyToMany) ].Count = 2").List()
				                	.Count);
			}
			Assert.AreEqual(1,
			                s.CreateQuery(
			                	"select c from c in class ContainerX where c.OneToMany[ c.ManyToMany[0].Count ].Name = 's'").List().
			                	Count);
			Assert.AreEqual(1,
			                s.CreateQuery(
			                	"select c from c in class ContainerX where c.ManyToMany[ c.OneToMany[0].Count ].Name = 's'").List().
			                	Count);

			s.Delete(c);
			s.Delete(s1);
			s.Delete(s2);
			s.Delete(s3);

			t.Commit();
			s.Close();
		}