示例#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 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();
		}