예제 #1
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();
		}
예제 #2
0
		public void PropertyRef()
		{
			object qid;
			object hid;

			using (ISession s = OpenSession())
			{
				Holder h = new Holder();
				h.Name = "foo";
				Holder h2 = new Holder();
				h2.Name = "bar";
				h.OtherHolder = h2;
				hid = s.Save(h);

				Qux q = new Qux();
				q.Holder = h2;
				qid = s.Save(q);
				s.Flush();
			}

			using (ISession s = OpenSession())
			{
				Holder h = (Holder) s.Load(typeof(Holder), hid);
				Assert.AreEqual(h.Name, "foo");
				Assert.AreEqual(h.OtherHolder.Name, "bar");
				object[] res =
					(object[]) s.CreateQuery("from Holder h join h.OtherHolder oh where h.OtherHolder.Name = 'bar'").List()[0];
				Assert.AreSame(h, res[0]);

				Qux q = (Qux) s.Get(typeof(Qux), qid);
				Assert.AreSame(q.Holder, h.OtherHolder);
				s.Delete(h);
				s.Delete(q);
				s.Flush();
			}
		}