Exemplo n.º 1
0
		public void ComplexCriteria()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Baz baz = new Baz();
			s.Save(baz);
			baz.SetDefaults();
			IDictionary topGlarchez = new Hashtable();
			baz.TopGlarchez = topGlarchez;
			Glarch g1 = new Glarch();
			g1.Name = "g1";
			s.Save(g1);
			Glarch g2 = new Glarch();
			g2.Name = "g2";
			s.Save(g2);

			g1.ProxyArray = new GlarchProxy[] {g2};
			topGlarchez['1'] = g1;
			topGlarchez['2'] = g2;
			Foo foo1 = new Foo();
			Foo foo2 = new Foo();
			s.Save(foo1);
			s.Save(foo2);
			baz.FooSet.Add(foo1);
			baz.FooSet.Add(foo2);
			baz.FooArray = new FooProxy[] {foo1};

			LockMode lockMode = (Dialect is DB2Dialect) ? LockMode.Read : LockMode.Upgrade;

			ICriteria crit = s.CreateCriteria(typeof(Baz));
			crit.CreateCriteria("TopGlarchez")
				.Add(Expression.IsNotNull("Name"))
				.CreateCriteria("ProxyArray")
				.Add(Expression.EqProperty("Name", "Name"))
				.Add(Expression.Eq("Name", "g2"))
				.Add(Expression.Gt("X", -666));
			crit.CreateCriteria("FooSet")
				.Add(Expression.IsNull("Null"))
				.Add(Expression.Eq("String", "a string"))
				.Add(Expression.Lt("Integer", -665));
			crit.CreateCriteria("FooArray")
				.Add(Expression.Eq("String", "a string"))
				.SetLockMode(lockMode);

			IList list = crit.List();
			Assert.AreEqual(2, list.Count);

			s.CreateCriteria(typeof(Glarch)).SetLockMode(LockMode.Upgrade).List();
			s.CreateCriteria(typeof(Glarch)).SetLockMode(CriteriaSpecification.RootAlias, LockMode.Upgrade).List();

			g2.Name = null;
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();

			crit = s.CreateCriteria(typeof(Baz))
				.SetLockMode(lockMode);
			crit.CreateCriteria("TopGlarchez")
				.Add(Expression.Gt("X", -666));
			crit.CreateCriteria("FooSet")
				.Add(Expression.IsNull("Null"));
			list = crit.List();

			Assert.AreEqual(4, list.Count);
			baz = (Baz) crit.UniqueResult();
			Assert.IsTrue(NHibernateUtil.IsInitialized(baz.TopGlarchez)); //cos it is nonlazy
			Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));

			//list = s.CreateCriteria(typeof( Baz ))
			//	.createCriteria("fooSet.foo.component.glarch")
			//		.Add( Expression.eq("name", "xxx") )
			//	.Add( Expression.eq("fooSet.foo.component.glarch.name", "xxx") )
			//	.list();
			//assertTrue( list.size()==0 );
			list = s.CreateCriteria(typeof(Baz))
				.CreateCriteria("FooSet")
				.CreateCriteria("TheFoo")
				.CreateCriteria("Component.Glarch")
				.Add(Expression.Eq("Name", "xxx"))
				.List();
			Assert.AreEqual(0, list.Count);

			list = s.CreateCriteria(typeof(Baz))
				.CreateAlias("FooSet", "foo")
				.CreateAlias("foo.TheFoo", "foo2")
				.SetLockMode("foo2", lockMode)
				.Add(Expression.IsNull("foo2.Component.Glarch"))
				.CreateCriteria("foo2.Component.Glarch")
				.Add(Expression.Eq("Name", "xxx"))
				.List();
			Assert.AreEqual(0, list.Count);

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

			s = OpenSession();
			t = s.BeginTransaction();

			crit = s.CreateCriteria(typeof(Baz));
			crit.CreateCriteria("TopGlarchez")
				.Add(Expression.IsNotNull("Name"));
			crit.CreateCriteria("FooSet")
				.Add(Expression.IsNull("Null"));

			list = crit.List();
			Assert.AreEqual(2, list.Count);
			baz = (Baz) crit.UniqueResult();
			Assert.IsTrue(NHibernateUtil.IsInitialized(baz.TopGlarchez)); //cos it is nonlazy
			Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
			s.Delete("from Glarch g");
			s.Delete(s.Get(typeof(Foo), foo1.Key));
			s.Delete(s.Get(typeof(Foo), foo2.Key));
			s.Delete(baz);
			t.Commit();
			s.Close();
		}
Exemplo n.º 2
0
		public void ObjectType()
		{
			ISession s = OpenSession();
			Parent g = new Parent();
			Foo foo = new Foo();
			g.Any = foo;
			s.Save(g);
			s.Save(foo);
			s.Flush();
			s.Close();

			s = OpenSession();
			g = (Parent) s.Load(typeof(Parent), g.Id);
			Assert.IsNotNull(g.Any);
			Assert.IsTrue(g.Any is FooProxy);
			s.Delete(g.Any);
			s.Delete(g);
			s.Flush();
			s.Close();
		}
Exemplo n.º 3
0
		public void ProxyReuse()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			FooProxy foo = new Foo();
			FooProxy foo2 = new Foo();
			object id = s.Save(foo);
			object id2 = s.Save(foo2);
			foo2.Int = 1234567;
			foo.Int = 1234;
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			foo = (FooProxy) s.Load(typeof(Foo), id);
			foo2 = (FooProxy) s.Load(typeof(Foo), id2);
			Assert.IsFalse(NHibernateUtil.IsInitialized(foo));
			NHibernateUtil.Initialize(foo2);
			NHibernateUtil.Initialize(foo);
			Assert.AreEqual(3, foo.Component.ImportantDates.Length);
			Assert.AreEqual(3, foo2.Component.ImportantDates.Length);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			foo.Float = 1.2f;
			foo2.Float = 1.3f;
			foo2.Dependent.Key = null;
			foo2.Component.Subcomponent.Fee.Key = null;
			Assert.IsFalse(foo2.Key.Equals(id));
			s.Save(foo, "xyzid");
			s.Update(foo2, id); //intentionally id, not id2!
			Assert.AreEqual(id, foo2.Key);
			Assert.AreEqual(1234567, foo2.Int);
			Assert.AreEqual("xyzid", foo.Key);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			foo = (FooProxy) s.Load(typeof(Foo), id);
			Assert.AreEqual(1234567, foo.Int);
			Assert.AreEqual(3, foo.Component.ImportantDates.Length);
			s.Delete(foo);
			s.Delete(s.Get(typeof(Foo), id2));
			s.Delete(s.Get(typeof(Foo), "xyzid"));
			Assert.AreEqual(3, s.Delete("from System.Object"));
			t.Commit();
			s.Close();

			string feekey = foo.Dependent.Key;
			s = OpenSession();
			t = s.BeginTransaction();
			foo.Component.Glarch = null; //no id property!
			s.Replicate(foo, ReplicationMode.Overwrite);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			Foo refoo = (Foo) s.Get(typeof(Foo), id);
			Assert.AreEqual(feekey, refoo.Dependent.Key);
			s.Delete(refoo);
			t.Commit();
			s.Close();
		}