Example #1
0
		public void DeleteOwner()
		{
			ISession s = OpenSession();
			Qux q = new Qux();
			s.Save(q);
			Fum f1 = new Fum(FumKey("f1"));
			Fum f2 = new Fum(FumKey("f2"));
			IList list = new ArrayList();
			list.Add(f1);
			list.Add(f2);
			f1.FumString = "f1";
			f2.FumString = "f2";
			q.Fums = new HashSet<Fum> {f1, f2};
			q.MoreFums = list;
			s.Save(f1);
			s.Save(f2);
			s.Flush();
			s.Close();

			s = OpenSession();
			ITransaction t = s.BeginTransaction();
			q = (Qux) s.Load(typeof(Qux), q.Key, LockMode.Upgrade);
			s.Lock(q, LockMode.Upgrade);
			s.Delete(q);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			list = s.CreateQuery("from fum in class NHibernate.DomainModel.Fum where not fum.FumString='FRIEND'").List();
			Assert.AreEqual(2, list.Count, "deleted owner");
			s.Lock(list[0], LockMode.Upgrade);
			s.Lock(list[1], LockMode.Upgrade);
			foreach (object obj in list)
			{
				s.Delete(obj);
			}
			t.Commit();
			s.Close();
		}
Example #2
0
		public void ComplicatedQuery()
		{
			ISession s = OpenSession();
			ITransaction txn = s.BeginTransaction();
			Foo foo = new Foo();
			object id = s.Save(foo);
			Assert.IsNotNull(id);
			Qux q = new Qux("q");
			foo.Dependent.Qux = q;
			s.Save(q);
			q.Foo.String = "foo2";

			IEnumerator enumer =
				s.CreateQuery("from foo in class Foo where foo.Dependent.Qux.Foo.String = 'foo2'").Enumerable().GetEnumerator();
			Assert.IsTrue(enumer.MoveNext());
			s.Delete(foo);
			txn.Commit();
			s.Close();
		}
Example #3
0
		public void CompositeIDCollections()
		{
			ISession s = OpenSession();
			Fum fum1 = new Fum(FumKey("fum1"));
			Fum fum2 = new Fum(FumKey("fum2"));
			fum1.FumString = "fee fo fi";
			fum2.FumString = "fee fo fi";
			s.Save(fum1);
			s.Save(fum2);
			Qux q = new Qux();
			s.Save(q);
			IList list = new ArrayList();
			list.Add(fum1);
			q.Fums = new HashSet<Fum> {fum1, fum2};
			q.MoreFums = list;
			fum1.QuxArray = new Qux[] {q};
			s.Flush();
			s.Close();

			s = OpenSession();
			q = (Qux) s.Load(typeof(Qux), q.Key);
			Assert.AreEqual(2, q.Fums.Count, "collection of fums");
			Assert.AreEqual(1, q.MoreFums.Count, "collection of fums");
			Assert.AreSame(q, ((Fum) q.MoreFums[0]).QuxArray[0], "unkeyed composite id collection");
			IEnumerator enumer = q.Fums.GetEnumerator();
			enumer.MoveNext();
			s.Delete((Fum) enumer.Current);
			enumer.MoveNext();
			s.Delete((Fum) enumer.Current);
			s.Delete(q);
			s.Flush();
			s.Close();
		}
Example #4
0
		public void UpdateFromTransient()
		{
			ISession s = OpenSession();
			Fee fee1 = new Fee();
			s.Save(fee1);
			Fee fee2 = new Fee();
			fee1.TheFee = fee2;
			fee2.TheFee = fee1;
			fee2.Fees = new HashedSet();
			Fee fee3 = new Fee();
			fee3.TheFee = fee1;
			fee3.AnotherFee = fee2;
			fee2.AnotherFee = fee3;
			s.Save(fee3);
			s.Save(fee2);
			s.Flush();
			s.Close();

			fee1.Fi = "changed";
			s = OpenSession();
			s.SaveOrUpdate(fee1);
			s.Flush();
			s.Close();

			Qux q = new Qux("quxxy");
			fee1.Qux = q;
			s = OpenSession();
			s.SaveOrUpdate(fee1);
			s.Flush();
			s.Close();

			s = OpenSession();
			fee1 = (Fee) s.Load(typeof(Fee), fee1.Key);
			Assert.AreEqual("changed", fee1.Fi, "updated from transient");
			Assert.IsNotNull(fee1.Qux, "unsaved-value");
			s.Delete(fee1.Qux);
			fee1.Qux = null;
			s.Flush();
			s.Close();

			fee2.Fi = "CHANGED";
			fee2.Fees.Add("an element");
			fee1.Fi = "changed again";
			s = OpenSession();
			s.SaveOrUpdate(fee2);
			s.Update(fee1, fee1.Key);
			s.Flush();
			s.Close();

			s = OpenSession();
			Fee fee = new Fee();
			s.Load(fee, fee2.Key);
			fee1 = (Fee) s.Load(typeof(Fee), fee1.Key);
			Assert.AreEqual("changed again", fee1.Fi, "updated from transient");
			Assert.AreEqual("CHANGED", fee.Fi, "updated from transient");
			Assert.IsTrue(fee.Fees.Contains("an element"), "updated collection");
			s.Flush();
			s.Close();

			fee.Fees.Clear();
			fee.Fees.Add("new element");
			fee1.TheFee = null;
			s = OpenSession();
			s.SaveOrUpdate(fee);
			s.SaveOrUpdate(fee1);
			s.Flush();
			s.Close();

			s = OpenSession();
			s.Load(fee, fee.Key);
			Assert.IsNotNull(fee.AnotherFee, "update");
			Assert.IsNotNull(fee.TheFee, "update");
			Assert.AreSame(fee.AnotherFee.TheFee, fee.TheFee, "update");
			Assert.IsTrue(fee.Fees.Contains("new element"), "updated collection");
			Assert.IsFalse(fee.Fees.Contains("an element"), "updated collection");
			s.Flush();
			s.Close();

			fee.Qux = new Qux("quxy");
			s = OpenSession();
			s.SaveOrUpdate(fee);
			s.Flush();
			s.Close();

			fee.Qux.Stuff = "xxx";
			s = OpenSession();
			s.SaveOrUpdate(fee);
			s.Flush();
			s.Close();

			s = OpenSession();
			s.Load(fee, fee.Key);
			Assert.IsNotNull(fee.Qux, "cascade update");
			Assert.AreEqual("xxx", fee.Qux.Stuff, "cascade update");
			Assert.IsNotNull(fee.AnotherFee, "update");
			Assert.IsNotNull(fee.TheFee, "update");
			Assert.AreSame(fee.AnotherFee.TheFee, fee.TheFee, "update");
			fee.AnotherFee.AnotherFee = null;
			s.Delete(fee);
			s.Delete("from fee in class NHibernate.DomainModel.Fee");
			s.Flush();
			s.Close();
		}
Example #5
0
		public void LazyCollections()
		{
			ISession s = OpenSession();
			Qux q = new Qux();
			s.Save(q);
			s.Flush();
			s.Close();

			s = OpenSession();
			q = (Qux) s.Load(typeof(Qux), q.Key);
			s.Flush();
			s.Close();

			// two exceptions are supposed to occur:")
			bool ok = false;
			try
			{
				int countMoreFums = q.MoreFums.Count;
			}
			catch (LazyInitializationException lie)
			{
				Debug.WriteLine("caught expected " + lie.ToString());
				ok = true;
			}
			Assert.IsTrue(ok, "lazy collection with one-to-many");

			ok = false;
			try
			{
				int countFums = q.Fums.Count;
			}
			catch (LazyInitializationException lie)
			{
				Debug.WriteLine("caught expected " + lie.ToString());
				ok = true;
			}

			Assert.IsTrue(ok, "lazy collection with many-to-many");

			s = OpenSession();
			q = (Qux) s.Load(typeof(Qux), q.Key);
			s.Delete(q);
			s.Flush();
			s.Close();
		}
Example #6
0
		public void Enumerable()
		{
			// this test used to be called Iterators()

			ISession s = OpenSession();
			for (int i = 0; i < 10; i++)
			{
				Qux q = new Qux();
				object qid = s.Save(q);
				Assert.IsNotNull(q, "q is not null");
				Assert.IsNotNull(qid, "qid is not null");
			}
			s.Flush();
			s.Close();

			s = OpenSession();
			IEnumerator enumer =
				s.CreateQuery("from q in class NHibernate.DomainModel.Qux where q.Stuff is null").Enumerable().GetEnumerator();
			int count = 0;
			while (enumer.MoveNext())
			{
				Qux q = (Qux) enumer.Current;
				q.Stuff = "foo";
				// can't remove item from IEnumerator in .net 
				if (count == 0 || count == 5)
				{
					s.Delete(q);
				}
				count++;
			}

			Assert.AreEqual(10, count, "found 10 items");
			s.Flush();
			s.Close();

			s = OpenSession();

			Assert.AreEqual(8,
			                s.Delete("from q in class NHibernate.DomainModel.Qux where q.Stuff=?", "foo", NHibernateUtil.String),
			                "delete by query");

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

			s = OpenSession();
			enumer = s.CreateQuery("from q in class NHibernate.DomainModel.Qux").Enumerable().GetEnumerator();
			Assert.IsFalse(enumer.MoveNext(), "no items in enumerator");
			s.Flush();
			s.Close();
		}
Example #7
0
		public void PersistentLifecycle()
		{
			ISession s = OpenSession();
			Qux q = new Qux();
			s.Save(q);
			q.Stuff = "foo bar baz qux";
			s.Flush();
			s.Close();

			s = OpenSession();
			q = (Qux) s.Load(typeof(Qux), q.Key);
			Assert.IsTrue(q.Created, "lifecycle create");
			Assert.IsTrue(q.Loaded, "lifecycle load");
			Assert.IsNotNull(q.Foo, "lifecycle subobject");
			s.Delete(q);
			Assert.IsTrue(q.Deleted, "lifecyle delete");
			s.Flush();
			s.Close();

			s = OpenSession();
			Assert.AreEqual(0, s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List().Count, "subdeletion");
			s.Flush();
			s.Close();
		}
Example #8
0
		public void Callback()
		{
			ISession s = OpenSession();
			Qux q = new Qux("0");
			s.Save(q);
			q.Child = (new Qux("1"));
			s.Save(q.Child);

			Qux q2 = new Qux("2");
			q2.Child = q.Child;

			Qux q3 = new Qux("3");
			q.Child.Child = q3;
			s.Save(q3);

			Qux q4 = new Qux("4");
			q4.Child = q3;

			s.Save(q4);
			s.Save(q2);
			s.Flush();
			s.Close();

			s = OpenSession();
			IList l = s.CreateQuery("from q in class NHibernate.DomainModel.Qux").List();
			Assert.AreEqual(5, l.Count);

			s.Delete(l[0]);
			s.Delete(l[1]);
			s.Delete(l[2]);
			s.Delete(l[3]);
			s.Delete(l[4]);
			s.Flush();
			s.Close();
		}
Example #9
0
		public void Load()
		{
			ISession s = OpenSession();
			Qux q = new Qux();
			s.Save(q);
			BarProxy b = new Bar();
			s.Save(b);
			s.Flush();
			s.Close();

			s = OpenSession();
			q = (Qux) s.Load(typeof(Qux), q.Key);
			b = (BarProxy) s.Load(typeof(Foo), b.Key);
			string tempKey = b.Key;
			Assert.IsFalse(NHibernateUtil.IsInitialized(b), "b should have been an unitialized Proxy");
			string tempString = b.BarString;
			Assert.IsTrue(NHibernateUtil.IsInitialized(b), "b should have been an initialized Proxy");
			BarProxy b2 = (BarProxy) s.Load(typeof(Bar), tempKey);
			Qux q2 = (Qux) s.Load(typeof(Qux), q.Key);
			Assert.AreSame(q, q2, "loaded same Qux");
			Assert.AreSame(b, b2, "loaded same BarProxy");
			s.Delete(q2);
			s.Delete(b2);
			s.Flush();
			s.Close();
		}
Example #10
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();
			}
		}