示例#1
0
		public void Query()
		{
			ISession s = OpenSession();
			ITransaction txn = s.BeginTransaction();
			Foo foo = new Foo();
			s.Save(foo);
			Foo foo2 = new Foo();
			s.Save(foo2);
			foo.TheFoo = foo2;

			IList list = s.CreateQuery("from Foo foo inner join fetch foo.TheFoo").List();
			Foo foof = (Foo) list[0];
			Assert.IsTrue(NHibernateUtil.IsInitialized(foof.TheFoo));

			list = s.CreateQuery("from Baz baz left outer join fetch baz.FooToGlarch").List();

			list = s.CreateQuery("select foo, bar from Foo foo left outer join foo.TheFoo bar where foo = ?")
				.SetEntity(0, foo).List();


			object[] row1 = (object[]) list[0];
			Assert.IsTrue(row1[0] == foo && row1[1] == foo2);

			s.CreateQuery("select foo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo = 'bar'").List();
			s.CreateQuery("select foo.TheFoo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo.TheFoo = 'bar'").List();
			s.CreateQuery("select foo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo.String = 'bar'").
				List();
			//			if( !( dialect is Dialect.HSQLDialect ) ) 
			//			{
			s.CreateQuery("select foo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo = foo.TheFoo.TheFoo").List();
			//			}
			s.CreateQuery(
				"select foo.String from foo in class Foo where foo.TheFoo.TheFoo = 'bar' and foo.TheFoo.TheFoo.TheFoo = 'baz'").List
				();
			s.CreateQuery(
				"select foo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo.String = 'a' and foo.TheFoo.String = 'b'").
				List();

			s.CreateQuery("from bar in class Bar, foo in elements(bar.Baz.FooArray)").List();

			if (Dialect is DB2Dialect)
			{
				s.CreateQuery("from foo in class Foo where lower( foo.TheFoo.String ) = 'foo'").List();
				s.CreateQuery("from foo in class Foo where lower( (foo.TheFoo.String || 'foo') || 'bar' ) = 'foo'").List();
				s.CreateQuery("from foo in class Foo where repeat( (foo.TheFoo.STring || 'foo') || 'bar', 2 ) = 'foo'").List();
				s.CreateQuery(
					"From foo in class Bar where foo.TheFoo.Integer is not null and repeat( (foo.TheFoo.String || 'foo') || 'bar', (5+5)/2 ) = 'foo'")
					.List();
				s.CreateQuery(
					"From foo in class Bar where foo.TheFoo.Integer is not null or repeat( (foo.TheFoo.String || 'foo') || 'bar', (5+5)/2 ) = 'foo'")
					.List();
			}

			if ((Dialect is SybaseDialect) || (Dialect is MsSql2000Dialect))
			{
				s.CreateQuery("select baz from Baz as baz join baz.FooArray foo group by baz order by sum(foo.Float)").Enumerable();
			}

			s.CreateQuery("from Foo as foo where foo.Component.Glarch.Name is not null").List();
			s.CreateQuery("from Foo as foo left outer join foo.Component.Glarch as glarch where glarch.Name = 'foo'").List();

			list = s.CreateQuery("from Foo").List();
			Assert.AreEqual(2, list.Count);
			Assert.IsTrue(list[0] is FooProxy);
			list = s.CreateQuery("from Foo foo left outer join foo.TheFoo").List();
			Assert.AreEqual(2, list.Count);
			Assert.IsTrue(((object[]) list[0])[0] is FooProxy);

			s.CreateQuery("From Foo, Bar").List();
			s.CreateQuery("from Baz baz left join baz.FooToGlarch, Bar bar join bar.TheFoo").List();
			s.CreateQuery("from Baz baz left join baz.FooToGlarch join baz.FooSet").List();
			s.CreateQuery("from Baz baz left join baz.FooToGlarch join fetch baz.FooSet foo left join fetch foo.TheFoo").List();

			list =
				s.CreateQuery(
					"from foo in class NHibernate.DomainModel.Foo where foo.String='osama bin laden' and foo.Boolean = true order by foo.String asc, foo.Component.Count desc")
					.List();
			Assert.AreEqual(0, list.Count, "empty query");
			IEnumerable enumerable =
				s.CreateQuery(
					"from foo in class NHibernate.DomainModel.Foo where foo.String='osama bin laden' order by foo.String asc, foo.Component.Count desc")
					.Enumerable();
			Assert.IsTrue(IsEmpty(enumerable), "empty enumerator");

			list = s.CreateQuery("select foo.TheFoo from foo in class NHibernate.DomainModel.Foo").List();
			Assert.AreEqual(1, list.Count, "query");
			Assert.AreEqual(foo.TheFoo, list[0], "returned object");
			foo.TheFoo.TheFoo = foo;
			foo.String = "fizard";

			if (Dialect.SupportsSubSelects)
			{
				if (!(Dialect is FirebirdDialect))
				{
					if (IsClassicParser)
					{
						list =
								s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where ? = some foo.Component.ImportantDates.elements")
										.SetDateTime(0, DateTime.Today).List();

					}
					else
					{
						list =
							s.CreateQuery(
								"from foo in class NHibernate.DomainModel.Foo where ? = some elements(foo.Component.ImportantDates)").
								SetDateTime(0, DateTime.Today).List();
					}
					Assert.AreEqual(2, list.Count, "component query");
				}

				list =
					s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where size(foo.Component.ImportantDates) = 3").List();
				Assert.AreEqual(2, list.Count, "component query");
				list = s.CreateQuery("from foo in class Foo where 0 = size(foo.Component.ImportantDates)").List();
				Assert.AreEqual(0, list.Count, "component query");
				list = s.CreateQuery("from foo in class Foo where exists elements(foo.Component.ImportantDates)").List();
				Assert.AreEqual(2, list.Count, "component query");
				s.CreateQuery("from foo in class Foo where not exists (from bar in class Bar where bar.id = foo.id)").List();

				s.CreateQuery(
					"select foo.TheFoo from foo in class Foo where foo = some(select x from x in class Foo where x.Long > foo.TheFoo.Long)")
					.List();
				s.CreateQuery(
					"from foo in class Foo where foo = some(select x from x in class Foo where x.Long > foo.TheFoo.Long) and foo.TheFoo.String='baz'")
					.List();
				s.CreateQuery(
					"from foo in class Foo where foo.TheFoo.String='baz' and foo = some(select x from x in class Foo where x.Long>foo.TheFoo.Long)")
					.List();
				s.CreateQuery("from foo in class Foo where foo = some(select x from x in class Foo where x.Long > foo.TheFoo.Long)")
					.List();

				s.CreateQuery(
					"select foo.String, foo.Date, foo.TheFoo.String, foo.id from foo in class Foo, baz in class Baz where foo in elements(baz.FooArray) and foo.String like 'foo'")
					.Enumerable();
			}

			list = s.CreateQuery("from foo in class Foo where foo.Component.Count is null order by foo.Component.Count").List();
			Assert.AreEqual(0, list.Count, "component query");

			list = s.CreateQuery("from foo in class Foo where foo.Component.Name='foo'").List();
			Assert.AreEqual(2, list.Count, "component query");

			list =
				s.CreateQuery(
					"select distinct foo.Component.Name, foo.Component.Name from foo in class Foo where foo.Component.Name='foo'").List
					();
			Assert.AreEqual(1, list.Count, "component query");

			list =
				s.CreateQuery("select distinct foo.Component.Name, foo.id from foo in class Foo where foo.Component.Name='foo'").
					List();
			Assert.AreEqual(2, list.Count, "component query");

			list = s.CreateQuery("select foo.TheFoo from foo in class Foo").List();
			Assert.AreEqual(2, list.Count, "query");

			list = s.CreateQuery("from foo in class Foo where foo.id=?").SetString(0, foo.Key).List();
			Assert.AreEqual(1, list.Count, "id query");

			list = s.CreateQuery("from foo in class Foo where foo.Key=?").SetString(0, foo.Key).List();
			Assert.AreEqual(1, list.Count, "named id query");
			Assert.AreSame(foo, list[0], "id query");

			list = s.CreateQuery("select foo.TheFoo from foo in class Foo where foo.String='fizard'").List();
			Assert.AreEqual(1, list.Count, "query");
			Assert.AreSame(foo.TheFoo, list[0], "returned object");

			list = s.CreateQuery("from foo in class Foo where foo.Component.Subcomponent.Name='bar'").List();
			Assert.AreEqual(2, list.Count, "components of components");

			list = s.CreateQuery("select foo.TheFoo from foo in class Foo where foo.TheFoo.id=?")
				.SetString(0, foo.TheFoo.Key).List();
			Assert.AreEqual(1, list.Count, "by id query");
			Assert.AreSame(foo.TheFoo, list[0], "by id returned object");

			s.CreateQuery("from foo in class Foo where foo.TheFoo = ?").SetEntity(0, foo.TheFoo).List();

			Assert.IsTrue(
				IsEmpty(s.CreateQuery("from bar in class Bar where bar.String='a string' or bar.String='a string'").Enumerable()
					));

			if (IsClassicParser)
			{
				enumerable = s.CreateQuery(
						"select foo.Component.Name, foo.Component.ImportantDates.elements from foo in class Foo where foo.TheFoo.id=?"
						).SetString(0, foo.TheFoo.Key).Enumerable();
			}
			else
			{
				enumerable =
					s.CreateQuery(
						"select foo.Component.Name, elements(foo.Component.ImportantDates) from foo in class Foo where foo.TheFoo.id=?").
						SetString(0, foo.TheFoo.Key).Enumerable();
			}

			int i = 0;
			foreach (object[] row in enumerable)
			{
				i++;
				Assert.IsTrue(row[0] is String);
				Assert.IsTrue(row[1] == null || row[1] is DateTime);
			}
			Assert.AreEqual(3, i); //WAS: 4

			if (IsClassicParser)
			{
				enumerable = s.CreateQuery(
						"select max(foo.Component.ImportantDates.elements) from foo in class Foo group by foo.id"
						).Enumerable();
			}
			else
			{
				enumerable =
					s.CreateQuery("select max(elements(foo.Component.ImportantDates)) from foo in class Foo group by foo.id").
						Enumerable();
			}
			IEnumerator enumerator = enumerable.GetEnumerator();

			Assert.IsTrue(enumerator.MoveNext());
			Assert.IsTrue(enumerator.Current is DateTime);

			list = s.CreateQuery(
				"select foo.TheFoo.TheFoo.TheFoo from foo in class Foo, foo2 in class Foo where"
				+ " foo = foo2.TheFoo and not not ( not foo.String='fizard' )"
				+ " and foo2.String between 'a' and (foo.TheFoo.String)"
				+ (Dialect is SQLiteDialect
				   	? " and ( foo2.String in ( 'fiz', 'blah') or 1=1 )"
				   	: " and ( foo2.String in ( 'fiz', 'blah', foo.TheFoo.String, foo.String, foo2.String ) )")
				).List();
			Assert.AreEqual(1, list.Count, "complex query");
			Assert.AreSame(foo, list[0], "returned object");

			foo.String = "from BoogieDown  -tinsel town  =!@#$^&*())";

			list = s.CreateQuery("from foo in class Foo where foo.String='from BoogieDown  -tinsel town  =!@#$^&*())'").List();
			Assert.AreEqual(1, list.Count, "single quotes");

			list = s.CreateQuery("from foo in class Foo where not foo.String='foo''bar'").List();
			Assert.AreEqual(2, list.Count, "single quotes");

			list = s.CreateQuery("from foo in class Foo where foo.Component.Glarch.Next is null").List();
			Assert.AreEqual(2, list.Count, "query association in component");

			Bar bar = new Bar();
			Baz baz = new Baz();
			baz.SetDefaults();
			bar.Baz = baz;
			baz.ManyToAny = new ArrayList();
			baz.ManyToAny.Add(bar);
			baz.ManyToAny.Add(foo);
			s.Save(bar);
			s.Save(baz);
			list =
				s.CreateQuery(" from bar in class Bar where bar.Baz.Count=667 and bar.Baz.Count!=123 and not bar.Baz.Name='1-E-1'").
					List();
			Assert.AreEqual(1, list.Count, "query many-to-one");
			list = s.CreateQuery(" from i in class Bar where i.Baz.Name='Bazza'").List();
			Assert.AreEqual(1, list.Count, "query many-to-one");

			if (DialectSupportsCountDistinct)
			{
				enumerable = s.CreateQuery("select count(distinct foo.TheFoo) from foo in class Foo").Enumerable();
				Assert.IsTrue(ContainsSingleObject(enumerable, (long) 2), "count"); // changed to Int64 (HQLFunction H3.2)
			}

			enumerable = s.CreateQuery("select count(foo.TheFoo.Boolean) from foo in class Foo").Enumerable();
			Assert.IsTrue(ContainsSingleObject(enumerable, (long) 2), "count"); // changed to Int64 (HQLFunction H3.2)

			enumerable = s.CreateQuery("select count(*), foo.Int from foo in class Foo group by foo.Int").Enumerable();
			enumerator = enumerable.GetEnumerator();
			Assert.IsTrue(enumerator.MoveNext());
			Assert.AreEqual(3L, (long) ((object[]) enumerator.Current)[0]);
			Assert.IsFalse(enumerator.MoveNext());

			enumerable = s.CreateQuery("select sum(foo.TheFoo.Int) from foo in class Foo").Enumerable();
			Assert.IsTrue(ContainsSingleObject(enumerable, (long) 4), "sum"); // changed to Int64 (HQLFunction H3.2)

			enumerable = s.CreateQuery("select count(foo) from foo in class Foo where foo.id=?")
				.SetString(0, foo.Key).Enumerable();
			Assert.IsTrue(ContainsSingleObject(enumerable, (long) 1), "id query count");

			list = s.CreateQuery("from foo in class Foo where foo.Boolean = ?").SetBoolean(0, true).List();

			list = s.CreateQuery("select new Foo(fo.X) from Fo fo").List();
			list = s.CreateQuery("select new Foo(fo.Integer) from Foo fo").List();

			list = s.CreateQuery("select new Foo(fo.X) from Foo fo")
				.SetCacheable(true)
				.List();
			Assert.IsTrue(list.Count == 3);
			list = s.CreateQuery("select new Foo(fo.X) from Foo fo")
				.SetCacheable(true)
				.List();
			Assert.IsTrue(list.Count == 3);

			enumerable = s.CreateQuery("select new Foo(fo.X) from Foo fo").Enumerable();
			enumerator = enumerable.GetEnumerator();
			Assert.IsTrue(enumerator.MoveNext(), "projection iterate (results)");
			Assert.IsTrue(typeof(Foo).IsAssignableFrom(enumerator.Current.GetType()),
			              "projection iterate (return check)");

			// TODO: ScrollableResults not implemented
			//ScrollableResults sr = s.CreateQuery("select new Foo(fo.x) from Foo fo").Scroll();
			//Assert.IsTrue( "projection scroll (results)", sr.next() );
			//Assert.IsTrue( "projection scroll (return check)", typeof(Foo).isAssignableFrom( sr.get(0).getClass() ) );

			list = s.CreateQuery("select foo.Long, foo.Component.Name, foo, foo.TheFoo from foo in class Foo").List();
			Assert.IsTrue(list.Count > 0);
			foreach (object[] row in list)
			{
				Assert.IsTrue(row[0] is long);
				Assert.IsTrue(row[1] is string);
				Assert.IsTrue(row[2] is Foo);
				Assert.IsTrue(row[3] is Foo);
			}

			if (DialectSupportsCountDistinct)
			{
				list =
					s.CreateQuery("select avg(foo.Float), max(foo.Component.Name), count(distinct foo.id) from foo in class Foo").List();
				Assert.IsTrue(list.Count > 0);
				foreach (object[] row in list)
				{
					Assert.IsTrue(row[0] is double); // changed from float to double (HQLFunction H3.2) 
					Assert.IsTrue(row[1] is string);
					Assert.IsTrue(row[2] is long); // changed from int to long (HQLFunction H3.2)
				}
			}

			list = s.CreateQuery("select foo.Long, foo.Component, foo, foo.TheFoo from foo in class Foo").List();
			Assert.IsTrue(list.Count > 0);
			foreach (object[] row in list)
			{
				Assert.IsTrue(row[0] is long);
				Assert.IsTrue(row[1] is FooComponent);
				Assert.IsTrue(row[2] is Foo);
				Assert.IsTrue(row[3] is Foo);
			}

			s.Save(new Holder("ice T"));
			s.Save(new Holder("ice cube"));

			Assert.AreEqual(15, s.CreateQuery("from o in class System.Object").List().Count);
			Assert.AreEqual(7, s.CreateQuery("from n in class INamed").List().Count);
			Assert.IsTrue(s.CreateQuery("from n in class INamed where n.Name is not null").List().Count == 4);

			foreach (INamed named in s.CreateQuery("from n in class INamed").Enumerable())
			{
				Assert.IsNotNull(named);
			}

			s.Save(new Holder("bar"));
			enumerable = s.CreateQuery("from n0 in class INamed, n1 in class INamed where n0.Name = n1.Name").Enumerable();
			int cnt = 0;
			foreach (object[] row in enumerable)
			{
				if (row[0] != row[1])
				{
					cnt++;
				}
			}

			//if ( !(dialect is Dialect.HSQLDialect) )
			//{
			Assert.IsTrue(cnt == 2);
			Assert.IsTrue(s.CreateQuery("from n0 in class INamed, n1 in class INamed where n0.Name = n1.Name").List().Count == 7);
			//}

			IQuery qu = s.CreateQuery("from n in class INamed where n.Name = :name");
			object temp = qu.ReturnTypes;
			temp = qu.NamedParameters;

			int c = 0;

			foreach (object obj in s.CreateQuery("from o in class System.Object").Enumerable())
			{
				c++;
			}
			Assert.IsTrue(c == 16);

			s.CreateQuery("select baz.Code, min(baz.Count) from baz in class Baz group by baz.Code").Enumerable();

			Assert.IsTrue(
				IsEmpty(
					s.CreateQuery(
						"selecT baz from baz in class Baz where baz.StringDateMap['foo'] is not null or baz.StringDateMap['bar'] = ?")
						.SetDateTime(0, DateTime.Today).Enumerable()));

			list = s.CreateQuery("select baz from baz in class Baz where baz.StringDateMap['now'] is not null").List();
			Assert.AreEqual(1, list.Count);

			list =
				s.CreateQuery("select baz from baz in class Baz where baz.StringDateMap[:now] is not null").SetString("now", "now").
					List();
			Assert.AreEqual(1, list.Count);

			list =
				s.CreateQuery(
					"select baz from baz in class Baz where baz.StringDateMap['now'] is not null and baz.StringDateMap['big bang'] < baz.StringDateMap['now']")
					.List();
			Assert.AreEqual(1, list.Count);

			list = s.CreateQuery("select index(date) from Baz baz join baz.StringDateMap date").List();
			Console.WriteLine(list);
			Assert.AreEqual(3, list.Count);

			s.CreateQuery(
				"from foo in class Foo where foo.Integer not between 1 and 5 and foo.String not in ('cde', 'abc') and foo.String is not null and foo.Integer<=3")
				.List();

			s.CreateQuery("from Baz baz inner join baz.CollectionComponent.Nested.Foos foo where foo.String is null").List();
			if (Dialect.SupportsSubSelects)
			{
				s.CreateQuery("from Baz baz inner join baz.FooSet where '1' in (from baz.FooSet foo where foo.String is not null)").
					List();
				s.CreateQuery(
					"from Baz baz where 'a' in elements(baz.CollectionComponent.Nested.Foos) and 1.0 in elements(baz.CollectionComponent.Nested.Floats)")
					.List();

				if (IsClassicParser)
				{
					s.CreateQuery(
							"from Baz baz where 'b' in baz.CollectionComponent.Nested.Foos.elements and 1.0 in baz.CollectionComponent.Nested.Floats.elements")
							.List();
				}
				else
				{
					s.CreateQuery(
						"from Baz baz where 'b' in elements(baz.CollectionComponent.Nested.Foos) and 1.0 in elements(baz.CollectionComponent.Nested.Floats)")
						.List();
				}
			}

			s.CreateQuery("from Foo foo join foo.TheFoo where foo.TheFoo in ('1','2','3')").List();

			//if ( !(dialect is Dialect.HSQLDialect) )
			s.CreateQuery("from Foo foo left join foo.TheFoo where foo.TheFoo in ('1','2','3')").List();
			s.CreateQuery("select foo.TheFoo from Foo foo where foo.TheFoo in ('1','2','3')").List();
			s.CreateQuery("select foo.TheFoo.String from Foo foo where foo.TheFoo in ('1','2','3')").List();
			s.CreateQuery("select foo.TheFoo.String from Foo foo where foo.TheFoo.String in ('1','2','3')").List();
			s.CreateQuery("select foo.TheFoo.Long from Foo foo where foo.TheFoo.String in ('1','2','3')").List();
			s.CreateQuery("select count(*) from Foo foo where foo.TheFoo.String in ('1','2','3') or foo.TheFoo.Long in (1,2,3)").
				List();
			s.CreateQuery("select count(*) from Foo foo where foo.TheFoo.String in ('1','2','3') group by foo.TheFoo.Long").List();

			s.CreateQuery("from Foo foo1 left join foo1.TheFoo foo2 left join foo2.TheFoo where foo1.String is not null").List();
			s.CreateQuery("from Foo foo1 left join foo1.TheFoo.TheFoo where foo1.String is not null").List();
			s.CreateQuery(
				"from Foo foo1 left join foo1.TheFoo foo2 left join foo1.TheFoo.TheFoo foo3 where foo1.String is not null").List();

			s.CreateQuery("select foo.Formula from Foo foo where foo.Formula > 0").List();

			int len = s.CreateQuery("from Foo as foo join foo.TheFoo as foo2 where foo2.id >'a' or foo2.id <'a'").List().Count;
			Assert.IsTrue(len == 2);

			s.Delete("from Holder");
			txn.Commit();
			s.Close();

			s = OpenSession();
			txn = s.BeginTransaction();
			baz = (Baz) s.CreateQuery("from Baz baz left outer join fetch baz.ManyToAny").UniqueResult();
			Assert.IsTrue(NHibernateUtil.IsInitialized(baz.ManyToAny));
			Assert.IsTrue(baz.ManyToAny.Count == 2);
			BarProxy barp = (BarProxy) baz.ManyToAny[0];
			s.CreateQuery("from Baz baz join baz.ManyToAny").List();
			Assert.IsTrue(s.CreateQuery("select baz from Baz baz join baz.ManyToAny a where index(a) = 0").List().Count == 1);

			FooProxy foop = (FooProxy) s.Get(typeof(Foo), foo.Key);
			Assert.IsTrue(foop == baz.ManyToAny[1]);

			barp.Baz = baz;
			Assert.IsTrue(s.CreateQuery("select bar from Bar bar where bar.Baz.StringDateMap['now'] is not null").List().Count ==
			              1);
			Assert.IsTrue(
				s.CreateQuery(
					"select bar from Bar bar join bar.Baz b where b.StringDateMap['big bang'] < b.StringDateMap['now'] and b.StringDateMap['now'] is not null")
					.List().Count == 1);
			Assert.IsTrue(
				s.CreateQuery(
					"select bar from Bar bar where bar.Baz.StringDateMap['big bang'] < bar.Baz.StringDateMap['now'] and bar.Baz.StringDateMap['now'] is not null")
					.List().Count == 1);

			list = s.CreateQuery("select foo.String, foo.Component, foo.id from Bar foo").List();
			Assert.IsTrue(((FooComponent) ((object[]) list[0])[1]).Name == "foo");
			list = s.CreateQuery("select elements(baz.Components) from Baz baz").List();
			Assert.IsTrue(list.Count == 2);
			list = s.CreateQuery("select bc.Name from Baz baz join baz.Components bc").List();
			Assert.IsTrue(list.Count == 2);
			//list = s.CreateQuery("select bc from Baz baz join baz.components bc").List();

			s.CreateQuery("from Foo foo where foo.Integer < 10 order by foo.String").SetMaxResults(12).List();

			s.Delete(barp);
			s.Delete(baz);
			s.Delete(foop.TheFoo);
			s.Delete(foop);
			txn.Commit();
			s.Close();
		}
示例#2
0
		public void Find()
		{
			ISession s = OpenSession();
			ITransaction txn = s.BeginTransaction();

			// some code commented out in h2.0.3

			Bar bar = new Bar();
			s.Save(bar);
			bar.BarString = "bar bar";
			bar.String = "xxx";
			Foo foo = new Foo();
			s.Save(foo);
			foo.String = "foo bar";
			s.Save(new Foo());
			s.Save(new Bar());

			IList list1 =
				s.CreateQuery("select foo from foo in class NHibernate.DomainModel.Foo where foo.String='foo bar'").List();
			Assert.AreEqual(1, list1.Count, "find size");
			Assert.AreSame(foo, list1[0], "find ==");

			IList list2 = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo order by foo.String, foo.Date").List();
			Assert.AreEqual(4, list2.Count, "find size");
			list1 = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where foo.class='B'").List();
			Assert.AreEqual(2, list1.Count, "class special property");
			list1 =
				s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where foo.class=NHibernate.DomainModel.Bar").List();
			Assert.AreEqual(2, list1.Count, "class special property");
			list1 = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where foo.class=Bar").List();
			list2 =
				s.CreateQuery(
					"select bar from bar in class NHibernate.DomainModel.Bar, foo in class NHibernate.DomainModel.Foo where bar.String = foo.String and not bar=foo")
					.List();

			Assert.AreEqual(2, list1.Count, "class special property");
			Assert.AreEqual(1, list2.Count, "select from a subclass");

			Trivial t = new Trivial();
			s.Save(t);
			txn.Commit();
			s.Close();

			s = OpenSession();
			txn = s.BeginTransaction();
			list1 = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where foo.String='foo bar'").List();
			Assert.AreEqual(1, list1.Count, "find count");
			// There is an interbase bug that causes null integers to return as 0, also numeric precision is <=15 -h2.0.3 comment
			Assert.IsTrue(((Foo) list1[0]).EqualsFoo(foo), "find equals");
			list2 = s.CreateQuery("select foo from foo in class NHibernate.DomainModel.Foo").List();
			Assert.AreEqual(5, list2.Count, "find count");
			IList list3 = s.CreateQuery("from bar in class NHibernate.DomainModel.Bar where bar.BarString='bar bar'").List();
			Assert.AreEqual(1, list3.Count, "find count");
			Assert.IsTrue(list2.Contains(list1[0]) && list2.Contains(list2[0]), "find same instance");
			Assert.AreEqual(1, s.CreateQuery("from t in class NHibernate.DomainModel.Trivial").List().Count);
			s.Delete("from t in class NHibernate.DomainModel.Trivial");

			list2 =
				s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where foo.Date = ?").SetDateTime(0,
				                                                                                             new DateTime(1970, 01,
				                                                                                                          01)).List();
			Assert.AreEqual(4, list2.Count, "find by date");
			IEnumerator enumer = list2.GetEnumerator();
			while (enumer.MoveNext())
			{
				s.Delete(enumer.Current);
			}

			list2 = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List();
			Assert.AreEqual(0, list2.Count, "find deleted");
			txn.Commit();
			s.Close();
		}
示例#3
0
		public void RecursiveLoad()
		{
			// Non polymorphisc class (there is an implementation optimization
			// being tested here) - from h2.0.3 - what does that mean?
			ISession s = OpenSession();
			ITransaction txn = s.BeginTransaction();
			GlarchProxy last = new Glarch();
			s.Save(last);
			last.Order = 0;
			for (int i = 0; i < 5; i++)
			{
				GlarchProxy next = new Glarch();
				s.Save(next);
				last.Next = next;
				last = next;
				last.Order = (short) (i + 1);
			}

			IEnumerator enumer = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch").Enumerable().GetEnumerator();
			while (enumer.MoveNext())
			{
				object objTemp = enumer.Current;
			}

			IList list = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch").List();
			Assert.AreEqual(6, list.Count, "recursive find");
			txn.Commit();
			s.Close();

			s = OpenSession();
			txn = s.BeginTransaction();
			list = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch").List();
			Assert.AreEqual(6, list.Count, "recursive iter");
			list = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch where g.Next is not null").List();
			Assert.AreEqual(5, list.Count, "exclude the null next");
			txn.Commit();
			s.Close();

			s = OpenSession();
			txn = s.BeginTransaction();
			enumer =
				s.CreateQuery("from g in class NHibernate.DomainModel.Glarch order by g.Order asc").Enumerable().GetEnumerator();
			while (enumer.MoveNext())
			{
				GlarchProxy g = (GlarchProxy) enumer.Current;
				Assert.IsNotNull(g, "not null");
				// no equiv in .net - so ran a delete query
				// iter.remove();
			}

			s.Delete("from NHibernate.DomainModel.Glarch as g");
			txn.Commit();
			s.Close();

			// same thing bug using polymorphic class (no optimization possible)
			s = OpenSession();
			txn = s.BeginTransaction();
			FooProxy flast = new Bar();
			s.Save(flast);
			for (int i = 0; i < 5; i++)
			{
				FooProxy foo = new Bar();
				s.Save(foo);
				flast.TheFoo = foo;
				flast = flast.TheFoo;
				flast.String = "foo" + (i + 1);
			}

			enumer = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").Enumerable().GetEnumerator();
			while (enumer.MoveNext())
			{
				object objTemp = enumer.Current;
			}

			list = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List();
			Assert.AreEqual(6, list.Count, "recursive find");
			txn.Commit();
			s.Close();

			s = OpenSession();
			txn = s.BeginTransaction();
			list = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List();
			Assert.AreEqual(6, list.Count, "recursive iter");
			enumer = list.GetEnumerator();
			while (enumer.MoveNext())
			{
				Assert.IsTrue(enumer.Current is BarProxy, "polymorphic recursive load");
			}
			txn.Commit();
			s.Close();

			s = OpenSession();
			txn = s.BeginTransaction();
			enumer =
				s.CreateQuery("from foo in class NHibernate.DomainModel.Foo order by foo.String asc").Enumerable().GetEnumerator();
			string currentString = String.Empty;

			while (enumer.MoveNext())
			{
				BarProxy bar = (BarProxy) enumer.Current;
				string theString = bar.String;
				Assert.IsNotNull(bar, "not null");
				if (currentString != String.Empty)
				{
					Assert.IsTrue(theString.CompareTo(currentString) >= 0, "not in asc order");
				}
				currentString = theString;
				// no equiv in .net - so made a hql delete
				// iter.remove();
			}

			s.Delete("from NHibernate.DomainModel.Foo as foo");
			txn.Commit();
			s.Close();
		}
示例#4
0
		public void Polymorphism()
		{
			ISession s = OpenSession();
			Bar bar = new Bar();
			s.Save(bar);
			bar.BarString = "bar bar";
			s.Flush();
			s.Close();

			s = OpenSession();
			FooProxy foo = (FooProxy) s.Load(typeof(Foo), bar.Key);
			Assert.IsTrue(foo is BarProxy, "polymorphic");
			Assert.IsTrue(((BarProxy) foo).BarString.Equals(bar.BarString), "subclass property");
			s.Delete(foo);
			s.Flush();
			s.Close();
		}
示例#5
0
		public void CollectionOfSelf()
		{
			ISession s = OpenSession();
			Bar bar = new Bar();
			s.Save(bar);
			// h2.0.3 was a set
			bar.Abstracts = new HashedSet();
			bar.Abstracts.Add(bar);
			Bar bar2 = new Bar();
			bar.Abstracts.Add(bar2);
			bar.TheFoo = bar;
			s.Save(bar2);
			s.Flush();
			s.Close();

			bar.Abstracts = null;
			s = OpenSession();
			s.Load(bar, bar.Key);

			Assert.AreEqual(2, bar.Abstracts.Count);
			Assert.IsTrue(bar.Abstracts.Contains(bar), "collection contains self");
			Assert.AreSame(bar, bar.TheFoo, "association to self");

			if (Dialect is MySQLDialect)
			{
				// Break the self-reference cycle to avoid error when deleting the row
				bar.TheFoo = null;
				s.Flush();
			}

			foreach (object obj in bar.Abstracts)
			{
				s.Delete(obj);
			}

			s.Flush();
			s.Close();
		}
		public void CollectionsInSelect()
		{
			ISession s = OpenSession();
			//			ITransaction t = s.BeginTransaction();
			Foo[ ] foos = new Foo[ ] {null, new Foo()};
			s.Save( foos[ 1 ] );
			Baz baz = new Baz();
			baz.SetDefaults();
			baz.FooArray = foos;
			s.Save( baz );
			Baz baz2 = new Baz();
			baz2.SetDefaults();
			s.Save( baz2 );

			Bar bar = new Bar();
			bar.Baz = baz;
			s.Save( bar );

			IList list = s.Find( "select new Result(foo.String, foo.Long, foo.Integer) from foo in class Foo" );
			Assert.AreEqual( 2, list.Count );
			Assert.IsTrue( list[ 0 ] is Result );
			Assert.IsTrue( list[ 1 ] is Result );

			list = s.Find( "select new Result( baz.Name, foo.Long, count(elements(baz.FooArray)) ) from Baz baz join baz.FooArray foo group by baz.Name, foo.Long" );
			Assert.AreEqual( 1, list.Count );
			Assert.IsTrue( list[ 0 ] is Result );
			Result r = ( Result ) list[ 0 ];

			Assert.AreEqual( baz.Name, r.Name );
			Assert.AreEqual( 1, r.Count );
			Assert.AreEqual( foos[ 1 ].Long, r.Amount );


			list = s.Find( "select new Result( baz.Name, max(foo.Long), count(foo) ) from Baz baz join baz.FooArray foo group by baz.Name" );
			Assert.AreEqual( 1, list.Count );
			Assert.IsTrue( list[ 0 ] is Result );
			r = ( Result ) list[ 0 ];
			Assert.AreEqual( baz.Name, r.Name );
			Assert.AreEqual( 1, r.Count );

			// TODO: figure out a better way
			// in hibernate this is hard coded as 696969696969696938l which is very dependant upon 
			// how the test are run because it is calculated on a global static variable...
			// maybe a better way to test this would be to assume that the first 
			//Assert.AreEqual( 696969696969696969L, r.Amount );

			s.Find( "select max( elements(bar.Baz.FooArray) ) from Bar as bar" );
			// the following test is disable for databases with no subselects... also for Interbase (not sure why) - comment from h2.0.3
			if( dialect.SupportsSubSelects )
			{
				s.Find( "select count(*) from Baz as baz where 1 in indices(baz.FooArray)" );
				s.Find( "select count(*) from Bar as bar where 'abc' in elements(bar.Baz.FooArray)" );
				s.Find( "select count(*) from Bar as bar where 1 in indices(bar.Baz.FooArray)" );
				if( !( dialect is Dialect.OracleDialect ) )
				{
					// These fail on Oracle 10 XE - g.id is a string (guid), indices are integer
					s.Find( "select count(*) from Bar as bar, bar.Component.Glarch.ProxyArray as g where g.id in indices(bar.Baz.FooArray)" );
					s.Find( "select max( elements(bar.Baz.FooArray) ) from Bar as bar, bar.Component.Glarch.ProxyArray as g where g.id in indices(bar.Baz.FooArray)" );
				}
				s.Find( "select count(*) from Bar as bar where 1 in (from bar.Component.Glarch.ProxyArray g where g.Name='foo')" );
				s.Find( "select count(*) from Bar as bar where 1 in (from g in bar.Component.Glarch.ProxyArray.elements where g.Name='foo')" );

				// TODO: figure out why this is throwing an ORA-1722 error
				if( !( dialect is Oracle9Dialect ) )
				{
					s.Find( "select count(*) from Bar as bar left outer join bar.Component.Glarch.ProxyArray as pg where 1 in (from g in bar.Component.Glarch.ProxyArray)" );
				}
			}

			list = s.Find( "from Baz baz left join baz.FooToGlarch join fetch baz.FooArray foo left join fetch foo.TheFoo" );
			Assert.AreEqual( 1, list.Count );
			Assert.AreEqual( 2, ( ( object[ ] ) list[ 0 ] ).Length );

			list = s.Find( "select baz.Name from Bar bar inner join bar.Baz baz inner join baz.FooSet foo where baz.Name = bar.String" );
			s.Find( "SELECT baz.Name FROM Bar AS bar INNER JOIN bar.Baz AS baz INNER JOIN baz.FooSet AS foo WHERE baz.Name = bar.String" );

			s.Find( "select baz.Name from Bar bar join bar.Baz baz left outer join baz.FooSet foo where baz.Name = bar.String" );

			s.Find( "select baz.Name from Bar bar, bar.Baz baz, baz.FooSet foo where baz.Name = bar.String" );
			s.Find( "SELECT baz.Name FROM Bar AS bar, bar.Baz AS baz, baz.FooSet AS foo WHERE baz.Name = bar.String" );

			s.Find( "select baz.Name from Bar bar left join bar.Baz baz left join baz.FooSet foo where baz.Name = bar.String" );
			s.Find( "select foo.String from Bar bar left join bar.Baz.FooSet foo where bar.String = foo.String" );

			s.Find( "select baz.Name from Bar bar left join bar.Baz baz left join baz.FooArray foo where baz.Name = bar.String" );
			s.Find( "select foo.String from Bar bar left join bar.Baz.FooArray foo where bar.String = foo.String" );

			s.Find( "select bar.String, foo.String from bar in class Bar inner join bar.Baz as baz inner join elements(baz.FooSet) as foo where baz.Name = 'name'" );
			s.Find( "select foo from bar in class Bar inner join bar.Baz as baz inner join baz.FooSet as foo" );
			s.Find( "select foo from bar in class Bar inner join bar.Baz.FooSet as foo" );

			s.Find( "select bar.String, foo.String from bar in class Bar, bar.Baz as baz, elements(baz.FooSet) as foo where baz.Name = 'name'" );
			s.Find( "select foo from bar in class Bar, bar.Baz as baz, baz.FooSet as foo" );
			s.Find( "select foo from bar in class Bar, bar.Baz.FooSet as foo" );

			Assert.AreEqual( 1, s.Find( "from Bar bar join bar.Baz.FooArray foo" ).Count );

			Assert.AreEqual( 0, s.Find( "from bar in class Bar, foo in bar.Baz.FooSet.elements" ).Count );
			Assert.AreEqual( 1, s.Find( "from bar in class Bar, foo in elements( bar.Baz.FooArray )" ).Count );

			s.Delete( bar );

			s.Delete( baz );
			s.Delete( baz2 );
			s.Delete( foos[ 1 ] );

			s.Flush();
			//			t.Commit();
			s.Close();
		}
示例#7
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();
		}
示例#8
0
		//[Ignore("TimeZone Portions commented out - http://jira.nhibernate.org:8080/browse/NH-88")]
		public void AssociationId()
		{
			string id;
			Bar bar;
			MoreStuff more;

			using (ISession s = OpenSession())
			{
				using (ITransaction t = s.BeginTransaction())
				{
					bar = new Bar();
					id = (string) s.Save(bar);
					more = new MoreStuff();
					more.Name = "More Stuff";
					more.IntId = 12;
					more.StringId = "id";
					Stuff stuf = new Stuff();
					stuf.MoreStuff = more;
					more.Stuffs = new ArrayList();
					more.Stuffs.Add(stuf);
					stuf.Foo = bar;
					stuf.Id = 1234;

					s.Save(more);
					t.Commit();
				}
			}

			using (ISession s = OpenSession())
			{
				using (ITransaction t = s.BeginTransaction())
				{
					//The special property (lowercase) id may be used to reference the unique identifier of an object. (You may also use its property name.) 
					string hqlString =
						"from s in class Stuff where s.Foo.id = ? and s.id.Id = ? and s.MoreStuff.id.IntId = ? and s.MoreStuff.id.StringId = ?";
					object[] values = new object[] {bar, (long) 1234, 12, "id"};
					IType[] types = new IType[]
						{
							NHibernateUtil.Entity(typeof(Foo)),
							NHibernateUtil.Int64,
							NHibernateUtil.Int32,
							NHibernateUtil.String
						};


					//IList results = s.List( hqlString, values, types );
					IQuery q = s.CreateQuery(hqlString);
					for (int i = 0; i < values.Length; i++)
					{
						q.SetParameter(i, values[i], types[i]);
					}
					IList results = q.List();
					Assert.AreEqual(1, results.Count);

					hqlString = "from s in class Stuff where s.Foo.id = ? and s.id.Id = ? and s.MoreStuff.Name = ?";
					values = new object[] {bar, (long) 1234, "More Stuff"};
					types = new IType[]
						{
							NHibernateUtil.Entity(typeof(Foo)),
							NHibernateUtil.Int64,
							NHibernateUtil.String
						};

					q = s.CreateQuery(hqlString);
					for (int i = 0; i < values.Length; i++)
					{
						q.SetParameter(i, values[i], types[i]);
					}
					results = q.List();
					Assert.AreEqual(1, results.Count);


					hqlString = "from s in class Stuff where s.Foo.String is not null";
					s.CreateQuery(hqlString).List();

					hqlString = "from s in class Stuff where s.Foo > '0' order by s.Foo";
					results = s.CreateQuery(hqlString).List();
					Assert.AreEqual(1, results.Count);

					t.Commit();
				}
			}

			FooProxy foo;

			using (ISession s = OpenSession())
			{
				using (ITransaction t = s.BeginTransaction())
				{
					foo = (FooProxy) s.Load(typeof(Foo), id);
					s.Load(more, more);
					t.Commit();
				}
			}

			using (ISession s = OpenSession())
			{
				using (ITransaction t = s.BeginTransaction())
				{
					Stuff stuff = new Stuff();
					stuff.Foo = foo;
					stuff.Id = 1234;
					stuff.MoreStuff = more;
					s.Load(stuff, stuff);

					Assert.AreEqual("More Stuff", stuff.MoreStuff.Name);
					s.Delete("from ms in class MoreStuff");
					s.Delete("from foo in class Foo");

					t.Commit();
				}
			}
		}
示例#9
0
		public void CollectionsInSelect()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Foo[] foos = new Foo[] {null, new Foo()};
			s.Save(foos[1]);
			Baz baz = new Baz();
			baz.SetDefaults();
			baz.FooArray = foos;
			s.Save(baz);
			Baz baz2 = new Baz();
			baz2.SetDefaults();
			s.Save(baz2);

			Bar bar = new Bar();
			bar.Baz = baz;
			s.Save(bar);

			IList list = s.CreateQuery("select new Result(foo.String, foo.Long, foo.Integer) from foo in class Foo").List();
			Assert.AreEqual(2, list.Count);
			Assert.IsTrue(list[0] is Result);
			Assert.IsTrue(list[1] is Result);

			list =
				s.CreateQuery(
					"select new Result( baz.Name, foo.Long, count(elements(baz.FooArray)) ) from Baz baz join baz.FooArray foo group by baz.Name, foo.Long")
					.List();
			Assert.AreEqual(1, list.Count);
			Assert.IsTrue(list[0] is Result);
			Result r = (Result) list[0];

			Assert.AreEqual(baz.Name, r.Name);
			Assert.AreEqual(1, r.Count);
			Assert.AreEqual(foos[1].Long, r.Amount);


			list =
				s.CreateQuery(
					"select new Result( baz.Name, max(foo.Long), count(foo) ) from Baz baz join baz.FooArray foo group by baz.Name").
					List();
			Assert.AreEqual(1, list.Count);
			Assert.IsTrue(list[0] is Result);
			r = (Result) list[0];
			Assert.AreEqual(baz.Name, r.Name);
			Assert.AreEqual(1, r.Count);

			s.CreateQuery("select max( elements(bar.Baz.FooArray) ) from Bar as bar").List();
			// the following test is disable for databases with no subselects... also for Interbase (not sure why) - comment from h2.0.3
			if (Dialect.SupportsSubSelects)
			{
				s.CreateQuery("select count(*) from Baz as baz where 1 in indices(baz.FooArray)").List();
				s.CreateQuery("select count(*) from Bar as bar where 'abc' in elements(bar.Baz.FooArray)").List();
				s.CreateQuery("select count(*) from Bar as bar where 1 in indices(bar.Baz.FooArray)").List();
				s.CreateQuery(
					"select count(*) from Bar as bar where '1' in (from bar.Component.Glarch.ProxyArray g where g.Name='foo')").List();
				
				// The nex query is wrong and is not present in H3.2:
				// The SQL result, from Classic parser, is the same of the previous query.
				// The AST parser has some problem to parse 'from g in bar.Component.Glarch.ProxyArray'
				// which should be parsed as 'from bar.Component.Glarch.ProxyArray g'
				//s.CreateQuery(
				//  "select count(*) from Bar as bar where '1' in (from g in bar.Component.Glarch.ProxyArray.elements where g.Name='foo')")
				//  .List();

				// TODO: figure out why this is throwing an ORA-1722 error
				// probably the conversion ProxyArray.id (to_number ensuring a not null value)
				if (!(Dialect is Oracle8iDialect))
				{
					s.CreateQuery(
						"select count(*) from Bar as bar join bar.Component.Glarch.ProxyArray as g where cast(g.id as Int32) in indices(bar.Baz.FooArray)").
						List();
					s.CreateQuery(
						"select max( elements(bar.Baz.FooArray) ) from Bar as bar join bar.Component.Glarch.ProxyArray as g where cast(g.id as Int32) in indices(bar.Baz.FooArray)")
						.List();
					s.CreateQuery(
						"select count(*) from Bar as bar left outer join bar.Component.Glarch.ProxyArray as pg where '1' in (from g in bar.Component.Glarch.ProxyArray)")
						.List();
				}
			}

			list =
				s.CreateQuery("from Baz baz left join baz.FooToGlarch join fetch baz.FooArray foo left join fetch foo.TheFoo").List();
			Assert.AreEqual(1, list.Count);
			Assert.AreEqual(2, ((object[]) list[0]).Length);

			list =
				s.CreateQuery(
					"select baz.Name from Bar bar inner join bar.Baz baz inner join baz.FooSet foo where baz.Name = bar.String").List();
			s.CreateQuery(
				"SELECT baz.Name FROM Bar AS bar INNER JOIN bar.Baz AS baz INNER JOIN baz.FooSet AS foo WHERE baz.Name = bar.String")
				.List();

			s.CreateQuery(
				"select baz.Name from Bar bar join bar.Baz baz left outer join baz.FooSet foo where baz.Name = bar.String").List();

			s.CreateQuery("select baz.Name from Bar bar join bar.Baz baz join baz.FooSet foo where baz.Name = bar.String").List();
			s.CreateQuery("SELECT baz.Name FROM Bar AS bar join bar.Baz AS baz join baz.FooSet AS foo WHERE baz.Name = bar.String").List();

			s.CreateQuery(
				"select baz.Name from Bar bar left join bar.Baz baz left join baz.FooSet foo where baz.Name = bar.String").List();
			s.CreateQuery("select foo.String from Bar bar left join bar.Baz.FooSet foo where bar.String = foo.String").List();

			s.CreateQuery(
				"select baz.Name from Bar bar left join bar.Baz baz left join baz.FooArray foo where baz.Name = bar.String").List();
			s.CreateQuery("select foo.String from Bar bar left join bar.Baz.FooArray foo where bar.String = foo.String").List();

			s.CreateQuery(
				"select bar.String, foo.String from bar in class Bar inner join bar.Baz as baz inner join elements(baz.FooSet) as foo where baz.Name = 'name'")
				.List();
			s.CreateQuery("select foo from bar in class Bar inner join bar.Baz as baz inner join baz.FooSet as foo").List();
			s.CreateQuery("select foo from bar in class Bar inner join bar.Baz.FooSet as foo").List();

			s.CreateQuery(
				"select bar.String, foo.String from bar in class Bar join bar.Baz as baz, elements(baz.FooSet) as foo where baz.Name = 'name'")
				.List();
			s.CreateQuery("select foo from bar in class Bar join bar.Baz as baz join baz.FooSet as foo").List();
			s.CreateQuery("select foo from bar in class Bar join bar.Baz.FooSet as foo").List();

			Assert.AreEqual(1, s.CreateQuery("from Bar bar join bar.Baz.FooArray foo").List().Count);

			if (IsClassicParser)
			{
				Assert.AreEqual(0, s.CreateQuery("from bar in class Bar, foo in bar.Baz.FooSet.elements").List().Count);
			}
			else
			{
				Assert.AreEqual(0, s.CreateQuery("from bar in class Bar, foo in elements(bar.Baz.FooSet)").List().Count);
			}

			Assert.AreEqual(1, s.CreateQuery("from bar in class Bar, foo in elements( bar.Baz.FooArray )").List().Count);

			s.Delete(bar);

			s.Delete(baz);
			s.Delete(baz2);
			s.Delete(foos[1]);
			t.Commit();
			s.Close();
		}
示例#10
0
		public void NamedParams()
		{
			Bar bar = new Bar();
			Bar bar2 = new Bar();
			bar.Name = "Bar";
			bar2.Name = "Bar Two";
			Baz baz = new Baz();
			baz.CascadingBars = new HashedSet();
			baz.CascadingBars.Add(bar);
			bar.Baz = baz;

			ISession s = OpenSession();
			ITransaction txn = s.BeginTransaction();
			s.Save(baz);
			s.Save(bar2);

			IList list =
				s.CreateQuery("from Bar bar left join bar.Baz baz left join baz.CascadingBars b where bar.Name like 'Bar %'").List();
			object row = list[0];
			Assert.IsTrue(row is object[] && ((object[]) row).Length == 3);

			IQuery q = s.CreateQuery(
				"select bar, b " +
				"from Bar bar " +
				"left join bar.Baz baz left join baz.CascadingBars b " +
				"where (bar.Name in (:nameList) or bar.Name in (:nameList)) and bar.String = :stringVal");

			IList nameList = new ArrayList();
			nameList.Add("bar");
			nameList.Add("Bar");
			nameList.Add("Bar Two");
			q.SetParameterList("nameList", nameList);
			q.SetParameter("stringVal", "a string");
			list = q.List();
			// a check for SAPDialect here
			Assert.AreEqual(2, list.Count);

			q = s.CreateQuery(
				"select bar, b " +
				"from Bar bar " +
				"inner join bar.Baz baz inner join baz.CascadingBars b " +
				"where bar.Name like 'Bar%'");
			list = q.List();
			Assert.AreEqual(1, list.Count);

			q = s.CreateQuery(
				"select bar, b " +
				"from Bar bar " +
				"left join bar.Baz baz left join baz.CascadingBars b " +
				"where bar.Name like :name and b.Name like :name");

			// add a check for HSQLDialect
			q.SetString("name", "Bar%");
			list = q.List();
			Assert.AreEqual(1, list.Count);

			s.Delete(baz);
			s.Delete(bar2);
			txn.Commit();
			s.Close();
		}
示例#11
0
		public void ComponentParent()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();

			BarProxy bar = new Bar();
			bar.Component = new FooComponent();
			Baz baz = new Baz();
			baz.Components = new FooComponent[] {new FooComponent(), new FooComponent()};
			s.Save(bar);
			s.Save(baz);
			t.Commit();
			s.Close();

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

			bar = (BarProxy) s.Load(typeof(Bar), bar.Key);
			s.Load(baz, baz.Code);

			Assert.AreEqual(bar, bar.BarComponent.Parent);
			Assert.IsTrue(baz.Components[0].Baz == baz && baz.Components[1].Baz == baz);

			s.Delete(baz);
			s.Delete(bar);

			t.Commit();
			s.Close();
		}
示例#12
0
		public void QueryLockMode()
		{
			ISession s = OpenSession();
			ITransaction tx = s.BeginTransaction();

			Bar bar = new Bar();
			Assert.IsNull(bar.Bytes);
			s.Save(bar);
			Assert.IsNotNull(bar.Bytes);
			s.Flush();
			Assert.IsNotNull(bar.Bytes);

			bar.String = "changed";
			Baz baz = new Baz();
			baz.Foo = bar;
			s.Save(baz);
			Assert.IsNotNull(bar.Bytes);

			IQuery q = s.CreateQuery("from Foo foo, Bar bar");
			q.SetLockMode("bar", LockMode.Upgrade);
			object[] result = (object[]) q.List()[0];
			Assert.IsNotNull(bar.Bytes);

			object b = result[0];

			Assert.IsTrue(s.GetCurrentLockMode(b) == LockMode.Write && s.GetCurrentLockMode(result[1]) == LockMode.Write);
			tx.Commit();
			Assert.IsNotNull(bar.Bytes);
			s.Disconnect();

			s.Reconnect();
			tx = s.BeginTransaction();
			Assert.IsNotNull(bar.Bytes);

			Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
			Assert.IsNotNull(bar.Bytes);
			s.CreateQuery("from Foo foo").List();
			Assert.IsNotNull(bar.Bytes);
			Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
			q = s.CreateQuery("from Foo foo");
			q.SetLockMode("foo", LockMode.Read);
			q.List();

			Assert.AreEqual(LockMode.Read, s.GetCurrentLockMode(b));
			s.Evict(baz);

			tx.Commit();
			s.Disconnect();

			s.Reconnect();
			tx = s.BeginTransaction();

			Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
			s.Delete(s.Load(typeof(Baz), baz.Code));
			Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));

			tx.Commit();
			s.Close();

			s = OpenSession();
			tx = s.BeginTransaction();
			q = s.CreateQuery("from Foo foo, Bar bar, Bar bar2");
			q.SetLockMode("bar", LockMode.Upgrade);
			q.SetLockMode("bar2", LockMode.Read);
			result = (object[]) q.List()[0];

			Assert.IsTrue(s.GetCurrentLockMode(result[0]) == LockMode.Upgrade &&
			              s.GetCurrentLockMode(result[1]) == LockMode.Upgrade);
			s.Delete(result[0]);
			tx.Commit();
			s.Close();
		}
示例#13
0
        public void CollectionsInSelect()
        {
            ISession s = OpenSession();
            ITransaction t = s.BeginTransaction();
            Foo[] foos = new Foo[] {null, new Foo()};
            s.Save(foos[1]);
            Baz baz = new Baz();
            baz.SetDefaults();
            baz.FooArray = foos;
            s.Save(baz);
            Baz baz2 = new Baz();
            baz2.SetDefaults();
            s.Save(baz2);

            Bar bar = new Bar();
            bar.Baz = baz;
            s.Save(bar);

            IList list = s.CreateQuery("select new Result(foo.String, foo.Long, foo.Integer) from foo in class Foo").List();
            Assert.AreEqual(2, list.Count);
            Assert.IsTrue(list[0] is Result);
            Assert.IsTrue(list[1] is Result);

            list =
                s.CreateQuery(
                    "select new Result( baz.Name, foo.Long, count(elements(baz.FooArray)) ) from Baz baz join baz.FooArray foo group by baz.Name, foo.Long")
                    .List();
            Assert.AreEqual(1, list.Count);
            Assert.IsTrue(list[0] is Result);
            Result r = (Result) list[0];

            Assert.AreEqual(baz.Name, r.Name);
            Assert.AreEqual(1, r.Count);
            Assert.AreEqual(foos[1].Long, r.Amount);

            list =
                s.CreateQuery(
                    "select new Result( baz.Name, max(foo.Long), count(foo) ) from Baz baz join baz.FooArray foo group by baz.Name").
                    List();
            Assert.AreEqual(1, list.Count);
            Assert.IsTrue(list[0] is Result);
            r = (Result) list[0];
            Assert.AreEqual(baz.Name, r.Name);
            Assert.AreEqual(1, r.Count);

            s.CreateQuery("select max( elements(bar.Baz.FooArray) ) from Bar as bar").List();
            // the following test is disable for databases with no subselects... also for Interbase (not sure why) - comment from h2.0.3
            if (Dialect.SupportsSubSelects)
            {
                s.CreateQuery("select count(*) from Baz as baz where 1 in indices(baz.FooArray)").List();
                s.CreateQuery("select count(*) from Bar as bar where 'abc' in elements(bar.Baz.FooArray)").List();
                s.CreateQuery("select count(*) from Bar as bar where 1 in indices(bar.Baz.FooArray)").List();
                s.CreateQuery(
                    "select count(*) from Bar as bar, bar.Component.Glarch.ProxyArray as g where g.id in indices(bar.Baz.FooArray)").
                    List();
                s.CreateQuery(
                    "select max( elements(bar.Baz.FooArray) ) from Bar as bar, bar.Component.Glarch.ProxyArray as g where g.id in indices(bar.Baz.FooArray)")
                    .List();
                s.CreateQuery(
                    "select count(*) from Bar as bar where 1 in (from bar.Component.Glarch.ProxyArray g where g.Name='foo')").List();
                s.CreateQuery(
                    "select count(*) from Bar as bar where 1 in (from g in bar.Component.Glarch.ProxyArray.elements where g.Name='foo')")
                    .List();

                // TODO: figure out why this is throwing an ORA-1722 error
                if (!(Dialect is Oracle9Dialect))
                {
                    s.CreateQuery(
                        "select count(*) from Bar as bar left outer join bar.Component.Glarch.ProxyArray as pg where 1 in (from g in bar.Component.Glarch.ProxyArray)")
                        .List();
                }
            }

            list =
                s.CreateQuery("from Baz baz left join baz.FooToGlarch join fetch baz.FooArray foo left join fetch foo.TheFoo").List();
            Assert.AreEqual(1, list.Count);
            Assert.AreEqual(2, ((object[]) list[0]).Length);

            list =
                s.CreateQuery(
                    "select baz.Name from Bar bar inner join bar.Baz baz inner join baz.FooSet foo where baz.Name = bar.String").List();
            s.CreateQuery(
                "SELECT baz.Name FROM Bar AS bar INNER JOIN bar.Baz AS baz INNER JOIN baz.FooSet AS foo WHERE baz.Name = bar.String")
                .List();

            s.CreateQuery(
                "select baz.Name from Bar bar join bar.Baz baz left outer join baz.FooSet foo where baz.Name = bar.String").List();

            s.CreateQuery("select baz.Name from Bar bar, bar.Baz baz, baz.FooSet foo where baz.Name = bar.String").List();
            s.CreateQuery("SELECT baz.Name FROM Bar AS bar, bar.Baz AS baz, baz.FooSet AS foo WHERE baz.Name = bar.String").List();

            s.CreateQuery(
                "select baz.Name from Bar bar left join bar.Baz baz left join baz.FooSet foo where baz.Name = bar.String").List();
            s.CreateQuery("select foo.String from Bar bar left join bar.Baz.FooSet foo where bar.String = foo.String").List();

            s.CreateQuery(
                "select baz.Name from Bar bar left join bar.Baz baz left join baz.FooArray foo where baz.Name = bar.String").List();
            s.CreateQuery("select foo.String from Bar bar left join bar.Baz.FooArray foo where bar.String = foo.String").List();

            s.CreateQuery(
                "select bar.String, foo.String from bar in class Bar inner join bar.Baz as baz inner join elements(baz.FooSet) as foo where baz.Name = 'name'")
                .List();
            s.CreateQuery("select foo from bar in class Bar inner join bar.Baz as baz inner join baz.FooSet as foo").List();
            s.CreateQuery("select foo from bar in class Bar inner join bar.Baz.FooSet as foo").List();

            s.CreateQuery(
                "select bar.String, foo.String from bar in class Bar, bar.Baz as baz, elements(baz.FooSet) as foo where baz.Name = 'name'")
                .List();
            s.CreateQuery("select foo from bar in class Bar, bar.Baz as baz, baz.FooSet as foo").List();
            s.CreateQuery("select foo from bar in class Bar, bar.Baz.FooSet as foo").List();

            Assert.AreEqual(1, s.CreateQuery("from Bar bar join bar.Baz.FooArray foo").List().Count);

            Assert.AreEqual(0, s.CreateQuery("from bar in class Bar, foo in bar.Baz.FooSet.elements").List().Count);
            Assert.AreEqual(1, s.CreateQuery("from bar in class Bar, foo in elements( bar.Baz.FooArray )").List().Count);

            s.Delete(bar);

            s.Delete(baz);
            s.Delete(baz2);
            s.Delete(foos[1]);
            t.Commit();
            s.Close();
        }
		public void Databinder()
		{
			BarProxy bar;
			FooProxy foo, foo2;
			Baz baz;

			using( ISession s = OpenSession() )
			{
				bar = new Bar();
				s.Save( bar );

				foo = new Foo();
				s.Save( foo );

				foo2 = new Foo();
				s.Save( foo2 );
				foo2.TheFoo = foo;

				baz = new Baz();
				s.Save( baz );
				baz.SetDefaults();
				baz.FooArray = new FooProxy[ ] {foo, foo2, bar, null};
				bar.TheFoo = foo;
				s.Flush();
			}

			using( ISession s = OpenSession() )
			{
				bar = ( BarProxy ) s.Load( typeof( Foo ), bar.Key );
				foo = ( FooProxy ) s.Load( typeof( Foo ), foo.Key );
				foo2 = ( FooProxy ) s.Load( typeof( Foo ), foo2.Key );
				baz = ( Baz ) s.Load( typeof( Baz ), baz.Code );

				IDatabinder binder = sessions.OpenDatabinder();
				binder.InitializeLazy = true;
				binder
					.Bind( foo2 )
					.Bind( baz )
					.Bind( foo )
					.Bind( bar );

				Console.WriteLine( binder.ToGenericXml() );

				binder
					.Bind( foo2 )
					.Bind( baz );

				Console.WriteLine( binder.ToXML() );

				//assertTrue( "dom", binder.toDOM()!=null );
				//assertTrue( "generic dom", binder.toGenericDOM()!=null );

				s.Delete( foo );
				s.Delete( baz );
				s.Delete( bar );
				s.Delete( foo2 );

				s.Flush();
			}
		}
示例#15
0
		public void Any()
		{
			ISession s = OpenSession();
			One one = new One();
			BarProxy foo = new Bar();
			foo.Object = one;
			object fid = s.Save(foo);
			object oid = one.Key;
			s.Flush();
			s.Close();

			s = OpenSession();
			IList list = s.CreateQuery("from Bar bar where bar.Object.id = ? and bar.Object.class = ?")
				.SetParameter(0, oid, NHibernateUtil.Int64).SetParameter(1, typeof(One).FullName, NHibernateUtil.ClassMetaType).List();
			Assert.AreEqual(1, list.Count);

			// this is a little different from h2.0.3 because the full type is stored, not
			// just the class name.
			list =
				s.CreateQuery(
					"select one from One one, Bar bar where bar.Object.id = one.id and bar.Object.class LIKE 'NHibernate.DomainModel.One%'")
					.List();
			Assert.AreEqual(1, list.Count);
			s.Flush();
			s.Close();

			s = OpenSession();
			foo = (BarProxy) s.Load(typeof(Foo), fid);
			Assert.IsNotNull(foo);
			Assert.IsTrue(foo.Object is One);
			Assert.AreEqual(oid, s.GetIdentifier(foo.Object));
			s.Delete(foo);
			s.Delete(foo.Object);
			s.Flush();
			s.Close();
		}
示例#16
0
		public void PersistCollections()
		{
			ISession s = OpenSession();
			ITransaction txn = s.BeginTransaction();
			IEnumerator enumer = s.CreateQuery("select count(*) from b in class Bar").Enumerable().GetEnumerator();
			enumer.MoveNext();
			Assert.AreEqual(0L, enumer.Current);

			Baz baz = new Baz();
			s.Save(baz);
			baz.SetDefaults();
			baz.StringArray = new string[] {"stuff"};
			ISet bars = new HashedSet();
			bars.Add(new Bar());
			baz.CascadingBars = bars;
			IDictionary sgm = new Hashtable();
			sgm["a"] = new Glarch();
			sgm["b"] = new Glarch();
			baz.StringGlarchMap = sgm;
			txn.Commit();
			s.Close();

			s = OpenSession();
			txn = s.BeginTransaction();
			baz = (Baz) ((object[]) s.CreateQuery("select baz, baz from baz in class NHibernate.DomainModel.Baz").List()[0])[1];
			Assert.AreEqual(1, baz.CascadingBars.Count, "baz.CascadingBars.Count");
			Foo foo = new Foo();
			s.Save(foo);
			Foo foo2 = new Foo();
			s.Save(foo2);
			baz.FooArray = new Foo[] {foo, foo, null, foo2};
			baz.FooSet.Add(foo);
			baz.Customs.Add(new string[] {"new", "custom"});
			baz.StringArray = null;
			baz.StringList[0] = "new value";
			baz.StringSet = new HashedSet();

			// NOTE: We put two items in the map, but expect only one to come back, because
			// of where="..." specified in the mapping for StringGlarchMap
			Assert.AreEqual(1, baz.StringGlarchMap.Count, "baz.StringGlarchMap.Count");
			IList list;

			// disable this for dbs with no subselects
			if (Dialect.SupportsSubSelects)
			{
				if (IsClassicParser)
				{
					list =
							s.CreateQuery(
									"select foo from foo in class NHibernate.DomainModel.Foo, baz in class NHibernate.DomainModel.Baz where foo in baz.FooArray.elements and 3 = some baz.IntArray.elements and 4 > all baz.IntArray.indices")
									.List();
				}
				else
				{
					list =
						s.CreateQuery(
							"select foo from foo in class NHibernate.DomainModel.Foo, baz in class NHibernate.DomainModel.Baz where foo in elements(baz.FooArray) and 3 = some elements(baz.IntArray) and 4 > all indices(baz.IntArray)")
							.List();
				}

				Assert.AreEqual(2, list.Count, "collection.elements find");
			}

			// sapdb doesn't like distinct with binary type
			//if( !(dialect is Dialect.SAPDBDialect) ) 
			//{
			if (IsClassicParser)
			{
				list =
						s.CreateQuery("select distinct foo from baz in class NHibernate.DomainModel.Baz, foo in baz.FooArray.elements").List
								();
			}
			else
			{
				list =
					s.CreateQuery("select distinct foo from baz in class NHibernate.DomainModel.Baz, foo in elements(baz.FooArray)").
						List();
			}
			Assert.AreEqual(2, list.Count, "collection.elements find");
			//}

			list = IsClassicParser
			       	? s.CreateQuery("select foo from baz in class NHibernate.DomainModel.Baz, foo in baz.FooSet.elements").List()
			       	: s.CreateQuery("select foo from baz in class NHibernate.DomainModel.Baz, foo in elements(baz.FooSet)").List();

			Assert.AreEqual(1, list.Count, "association.elements find");

			txn.Commit();
			s.Close();

			s = OpenSession();
			txn = s.BeginTransaction();
			baz = (Baz)s.CreateQuery("select baz from baz in class NHibernate.DomainModel.Baz order by baz").List()[0];
			Assert.AreEqual(4, baz.Customs.Count, "collection of custom types - added element");
			Assert.IsNotNull(baz.Customs[0], "collection of custom types - added element");
			Assert.IsNotNull(baz.Components[1].Subcomponent, "component of component in collection");
			Assert.AreSame(baz, baz.Components[1].Baz);

			IEnumerator fooSetEnumer = baz.FooSet.GetEnumerator();
			fooSetEnumer.MoveNext();
			Assert.IsTrue(((FooProxy) fooSetEnumer.Current).Key.Equals(foo.Key), "set of objects");
			Assert.AreEqual(0, baz.StringArray.Length, "collection removed");
			Assert.AreEqual("new value", baz.StringList[0], "changed element");
			Assert.AreEqual(0, baz.StringSet.Count, "replaced set");

			baz.StringSet.Add("two");
			baz.StringSet.Add("one");
			baz.Bag.Add("three");
			txn.Commit();
			s.Close();

			s = OpenSession();
			txn = s.BeginTransaction();
			baz = (Baz)s.CreateQuery("select baz from baz in class NHibernate.DomainModel.Baz order by baz").List()[0];
			Assert.AreEqual(2, baz.StringSet.Count);
			int index = 0;
			foreach (string key in baz.StringSet)
			{
				// h2.0.3 doesn't have this because the Set has a first() and last() method
				index++;
				if (index == 1)
				{
					Assert.AreEqual("one", key);
				}
				if (index == 2)
				{
					Assert.AreEqual("two", key);
				}
				if (index > 2)
				{
					Assert.Fail("should not be more than 2 items in StringSet");
				}
			}
			Assert.AreEqual(5, baz.Bag.Count);
			baz.StringSet.Remove("two");
			baz.Bag.Remove("duplicate");
			txn.Commit();
			s.Close();

			s = OpenSession();
			txn = s.BeginTransaction();
			baz = (Baz)s.Load(typeof(Baz), baz.Code);
			Bar bar = new Bar();
			Bar bar2 = new Bar();
			s.Save(bar);
			s.Save(bar2);
			baz.TopFoos = new HashedSet();
			baz.TopFoos.Add(bar);
			baz.TopFoos.Add(bar2);
			baz.TopGlarchez = new Hashtable();
			GlarchProxy g = new Glarch();
			s.Save(g);
			baz.TopGlarchez['G'] = g;
			Hashtable map = new Hashtable();
			map[bar] = g;
			map[bar2] = g;
			baz.FooToGlarch = map;
			map = new Hashtable();
			map[new FooComponent("name", 123, null, null)] = bar;
			map[new FooComponent("nameName", 12, null, null)] = bar;
			baz.FooComponentToFoo = map;
			map = new Hashtable();
			map[bar] = g;
			baz.GlarchToFoo = map;
			txn.Commit();
			s.Close();

			using(s = OpenSession())
			using (txn = s.BeginTransaction())
			{
				baz = (Baz) s.CreateQuery("select baz from baz in class NHibernate.DomainModel.Baz order by baz").List()[0];
				ISession s2 = OpenSession();
				ITransaction txn2 = s2.BeginTransaction();
				baz = (Baz) s.CreateQuery("select baz from baz in class NHibernate.DomainModel.Baz order by baz").List()[0];
				object o = baz.FooComponentToFoo[new FooComponent("name", 123, null, null)];
				Assert.IsNotNull(o);
				Assert.AreEqual(o, baz.FooComponentToFoo[new FooComponent("nameName", 12, null, null)]);
				txn2.Commit();
				s2.Close();
				Assert.AreEqual(2, baz.TopFoos.Count);
				Assert.AreEqual(1, baz.TopGlarchez.Count);
				enumer = baz.TopFoos.GetEnumerator();
				Assert.IsTrue(enumer.MoveNext());
				Assert.IsNotNull(enumer.Current);
				Assert.AreEqual(1, baz.StringSet.Count);
				Assert.AreEqual(4, baz.Bag.Count);
				Assert.AreEqual(2, baz.FooToGlarch.Count);
				Assert.AreEqual(2, baz.FooComponentToFoo.Count);
				Assert.AreEqual(1, baz.GlarchToFoo.Count);

				enumer = baz.FooToGlarch.Keys.GetEnumerator();
				for (int i = 0; i < 2; i++)
				{
					enumer.MoveNext();
					Assert.IsTrue(enumer.Current is BarProxy);
				}
				enumer = baz.FooComponentToFoo.Keys.GetEnumerator();
				enumer.MoveNext();
				FooComponent fooComp = (FooComponent) enumer.Current;
				Assert.IsTrue((fooComp.Count == 123 && fooComp.Name.Equals("name"))
				              || (fooComp.Count == 12 && fooComp.Name.Equals("nameName")));
				Assert.IsTrue(baz.FooComponentToFoo[fooComp] is BarProxy);

				Glarch g2 = new Glarch();
				s.Save(g2);
				g = (GlarchProxy) baz.TopGlarchez['G'];
				baz.TopGlarchez['H'] = g;
				baz.TopGlarchez['G'] = g2;
				txn.Commit();
				s.Close();
			}

			s = OpenSession();
			txn = s.BeginTransaction();
			baz = (Baz)s.CreateQuery("select baz from baz in class NHibernate.DomainModel.Baz order by baz").List()[0];
			Assert.AreEqual(2, baz.TopGlarchez.Count);
			txn.Commit();
			s.Disconnect();

			// serialize and then deserialize the session.
			Stream stream = new MemoryStream();
			IFormatter formatter = new BinaryFormatter();
			formatter.Serialize(stream, s);

			s.Close();

			stream.Position = 0;
			s = (ISession) formatter.Deserialize(stream);
			stream.Close();

			s.Reconnect();
			txn = s.BeginTransaction();
			baz = (Baz) s.Load(typeof(Baz), baz.Code);
			s.Delete(baz);
			s.Delete(baz.TopGlarchez['G']);
			s.Delete(baz.TopGlarchez['H']);

			IDbCommand cmd = s.Connection.CreateCommand();
			s.Transaction.Enlist(cmd);
			cmd.CommandText = "update " + Dialect.QuoteForTableName("glarchez") + " set baz_map_id=null where baz_map_index='a'";
			int rows = cmd.ExecuteNonQuery();
			Assert.AreEqual(1, rows);
			Assert.AreEqual(2, s.Delete("from bar in class NHibernate.DomainModel.Bar"));
			FooProxy[] arr = baz.FooArray;
			Assert.AreEqual(4, arr.Length);
			Assert.AreEqual(foo.Key, arr[1].Key);
			for (int i = 0; i < arr.Length; i++)
			{
				if (arr[i] != null)
				{
					s.Delete(arr[i]);
				}
			}

			try
			{
				s.Load(typeof(Qux), (long) 666); //nonexistent
			}
			catch (ObjectNotFoundException onfe)
			{
				Assert.IsNotNull(onfe, "should not find a Qux with id of 666 when Proxies are not implemented.");
			}

			Assert.AreEqual(1, s.Delete("from g in class Glarch"), "Delete('from g in class Glarch')");
			txn.Commit();
			s.Disconnect();

			// serialize and then deserialize the session.
			stream = new MemoryStream();
			formatter.Serialize(stream, s);

			s.Close();

			stream.Position = 0;
			s = (ISession) formatter.Deserialize(stream);
			stream.Close();

			Qux nonexistentQux = (Qux) s.Load(typeof(Qux), (long) 666); //nonexistent
			Assert.IsNotNull(nonexistentQux, "even though it doesn't exists should still get a proxy - no db hit.");

			s.Close();
		}
示例#17
0
		public void ProxiesInCollections()
		{
			ISession s = OpenSession();
			Baz baz = new Baz();
			Bar bar = new Bar();
			Bar bar2 = new Bar();
			s.Save(bar);
			object bar2id = s.Save(bar2);
			baz.FooArray = new Foo[] {bar, bar2};

			ISet hashset = new HashedSet();
			bar = new Bar();
			s.Save(bar);
			hashset.Add(bar);
			baz.FooSet = hashset;
			hashset = new HashedSet();
			hashset.Add(new Bar());
			hashset.Add(new Bar());
			baz.CascadingBars = hashset;
			ArrayList list = new ArrayList();
			list.Add(new Foo());
			baz.FooBag = list;
			object id = s.Save(baz);
			IEnumerator enumer = baz.CascadingBars.GetEnumerator();
			enumer.MoveNext();
			object bid = ((Bar) enumer.Current).Key;
			s.Flush();
			s.Close();

			s = OpenSession();
			BarProxy barprox = (BarProxy) s.Load(typeof(Bar), bid);
			BarProxy bar2prox = (BarProxy) s.Load(typeof(Bar), bar2id);
			Assert.IsTrue(bar2prox is INHibernateProxy);
			Assert.IsTrue(barprox is INHibernateProxy);
			baz = (Baz) s.Load(typeof(Baz), id);
			enumer = baz.CascadingBars.GetEnumerator();
			enumer.MoveNext();
			BarProxy b1 = (BarProxy) enumer.Current;
			enumer.MoveNext();
			BarProxy b2 = (BarProxy) enumer.Current;
			Assert.IsTrue(
				(b1 == barprox && !(b2 is INHibernateProxy))
				|| (b2 == barprox && !(b1 is INHibernateProxy))); //one-to-many
			Assert.IsTrue(baz.FooArray[0] is INHibernateProxy); //many-to-many
			Assert.AreEqual(bar2prox, baz.FooArray[1]);
			if (sessions.Settings.IsOuterJoinFetchEnabled)
			{
				enumer = baz.FooBag.GetEnumerator();
				enumer.MoveNext();
				Assert.IsFalse(enumer.Current is INHibernateProxy); // many-to-many outer-join="true"
			}

			enumer = baz.FooSet.GetEnumerator();
			enumer.MoveNext();
			Assert.IsFalse(enumer.Current is INHibernateProxy); //one-to-many
			s.Delete("from o in class Baz");
			s.Delete("from o in class Foo");
			s.Flush();
			s.Close();
		}
		//[Ignore("TimeZone Portions commented out - http://jira.nhibernate.org:8080/browse/NH-88")]
			public void AssociationId()
		{
			string id;
			Bar bar;
			MoreStuff more;

			using( ISession s = OpenSession() )
			{
				using( ITransaction t = s.BeginTransaction() )
				{
					bar = new Bar();
					id = ( string ) s.Save( bar );
					more = new MoreStuff();
					more.Name = "More Stuff";
					more.IntId = 12;
					more.StringId = "id";
					Stuff stuf = new Stuff();
					stuf.MoreStuff = more;
					more.Stuffs = new ArrayList();
					more.Stuffs.Add( stuf );
					stuf.Foo = bar;
					stuf.Id = 1234;

					//TODO: http://jira.nhibernate.org:8080/browse/NH-88
					//stuf.setProperty(TimeZone.getDefault() );
					s.Save( more );
					t.Commit();
				}
			}

			using( ISession s = OpenSession() )
			{
				using( ITransaction t = s.BeginTransaction() )
				{
					//The special property (lowercase) id may be used to reference the unique identifier of an object. (You may also use its property name.) 
					string hqlString = "from s in class Stuff where s.Foo.id = ? and s.id.Id = ? and s.MoreStuff.id.IntId = ? and s.MoreStuff.id.StringId = ?";
					object[ ] values = new object[ ] {bar, ( long ) 1234, 12, "id"};
					IType[ ] types = new IType[ ]
						{
							NHibernateUtil.Entity( typeof( Foo ) ),
							NHibernateUtil.Int64,
							NHibernateUtil.Int32,
							NHibernateUtil.String
						};


					IList results = s.Find( hqlString, values, types );
					Assert.AreEqual( 1, results.Count );

					hqlString = "from s in class Stuff where s.Foo.id = ? and s.id.Id = ? and s.MoreStuff.Name = ?";
					values = new object[ ] {bar, ( long ) 1234, "More Stuff"};
					types = new IType[ ]
						{
							NHibernateUtil.Entity( typeof( Foo ) ),
							NHibernateUtil.Int64,
							NHibernateUtil.String
						};

					results = s.Find( hqlString, values, types );
					Assert.AreEqual( 1, results.Count );


					hqlString = "from s in class Stuff where s.Foo.String is not null";
					s.Find( hqlString );

					hqlString = "from s in class Stuff where s.Foo > '0' order by s.Foo";
					results = s.Find( hqlString );
					Assert.AreEqual( 1, results.Count );

					t.Commit();
				}
			}

			FooProxy foo;

			using( ISession s = OpenSession() )
			{
				using( ITransaction t = s.BeginTransaction() )
				{
					foo = ( FooProxy ) s.Load( typeof( Foo ), id );
					s.Load( more, more );
					t.Commit();
				}
			}

			using( ISession s = OpenSession() )
			{
				using( ITransaction t = s.BeginTransaction() )
				{
					Stuff stuff = new Stuff();
					stuff.Foo = foo;
					stuff.Id = 1234;
					stuff.MoreStuff = more;
					s.Load( stuff, stuff );

					// TODO: figure out what to do with TimeZone
					// http://jira.nhibernate.org:8080/browse/NH-88
					//Assert.IsTrue( stuff.getProperty().equals( TimeZone.getDefault() ) );
					Assert.AreEqual( "More Stuff", stuff.MoreStuff.Name );
					s.Delete( "from ms in class MoreStuff" );
					s.Delete( "from foo in class Foo" );

					t.Commit();
				}
			}

		}