public void Copy() { Baz baz = new Baz(); baz.SetDefaults(); using (ISession s = OpenSession()) { Baz persistentBaz = new Baz(); s.Save(persistentBaz); s.Flush(); baz.Code = persistentBaz.Code; } using (ISession s = OpenSession()) { Baz persistentBaz = s.Get(typeof(Baz), baz.Code) as Baz; Baz copiedBaz = (Baz) s.SaveOrUpdateCopy(baz); Assert.AreSame(persistentBaz, copiedBaz); s.Delete(persistentBaz); s.Flush(); } }
public void AutoFlushCollections() { ISession s = OpenSession(); ITransaction tx = s.BeginTransaction(); Baz baz = new Baz(); baz.SetDefaults(); s.Save(baz); tx.Commit(); s.Close(); s = OpenSession(); tx = s.BeginTransaction(); baz = (Baz) s.Load(typeof(Baz), baz.Code); baz.StringArray[0] = "bark"; IEnumerator e; if (IsClassicParser) { e = s.CreateQuery("select baz.StringArray.elements from baz in class NHibernate.DomainModel.Baz").Enumerable(). GetEnumerator(); } else { e = s.CreateQuery("select elements(baz.StringArray) from baz in class NHibernate.DomainModel.Baz").Enumerable(). GetEnumerator(); } bool found = false; while (e.MoveNext()) { if ("bark".Equals(e.Current)) { found = true; } } Assert.IsTrue(found); baz.StringArray = null; if (IsClassicParser) { e = s.CreateQuery("select distinct baz.StringArray.elements from baz in class NHibernate.DomainModel.Baz") .Enumerable() .GetEnumerator(); } else { e = s.CreateQuery("select distinct elements(baz.StringArray) from baz in class NHibernate.DomainModel.Baz").Enumerable() .GetEnumerator(); } Assert.IsFalse(e.MoveNext()); baz.StringArray = new string[] {"foo", "bar"}; if (IsClassicParser) { e = s.CreateQuery("select baz.StringArray.elements from baz in class NHibernate.DomainModel.Baz") .Enumerable() .GetEnumerator(); } else { e = s.CreateQuery("select elements(baz.StringArray) from baz in class NHibernate.DomainModel.Baz").Enumerable(). GetEnumerator(); } Assert.IsTrue(e.MoveNext()); Foo foo = new Foo(); s.Save(foo); s.Flush(); baz.FooArray = new Foo[] {foo}; if (IsClassicParser) { e = s.CreateQuery("select foo from baz in class NHibernate.DomainModel.Baz, foo in baz.FooArray.elements") .Enumerable() .GetEnumerator(); } else { e = s.CreateQuery("select foo from baz in class NHibernate.DomainModel.Baz, foo in elements(baz.FooArray)").Enumerable() .GetEnumerator(); } found = false; while (e.MoveNext()) { if (foo == e.Current) { found = true; } } Assert.IsTrue(found); baz.FooArray[0] = null; if (IsClassicParser) { e = s.CreateQuery("select foo from baz in class NHibernate.DomainModel.Baz, foo in baz.FooArray.elements") .Enumerable() .GetEnumerator(); } else { e = s.CreateQuery("select foo from baz in class NHibernate.DomainModel.Baz, foo in elements(baz.FooArray)").Enumerable() .GetEnumerator(); } Assert.IsFalse(e.MoveNext()); baz.FooArray[0] = foo; if (IsClassicParser) { e = s.CreateQuery("select baz.FooArray.elements from baz in class NHibernate.DomainModel.Baz") .Enumerable() .GetEnumerator(); } else { e = s.CreateQuery("select elements(baz.FooArray) from baz in class NHibernate.DomainModel.Baz").Enumerable(). GetEnumerator(); } Assert.IsTrue(e.MoveNext()); if (Dialect.SupportsSubSelects && !(Dialect is FirebirdDialect)) { baz.FooArray[0] = null; if (IsClassicParser) { e = s.CreateQuery("from baz in class NHibernate.DomainModel.Baz where ? in baz.FooArray.elements") .SetEntity(0, foo).Enumerable().GetEnumerator(); } else { e = s.CreateQuery("from baz in class NHibernate.DomainModel.Baz where ? in elements(baz.FooArray)").SetEntity(0, foo). Enumerable().GetEnumerator(); } Assert.IsFalse(e.MoveNext()); baz.FooArray[0] = foo; if (IsClassicParser) { e = s.CreateQuery("select foo from foo in class NHibernate.DomainModel.Foo where foo in " + "(select elt from baz in class NHibernate.DomainModel.Baz, elt in baz.FooArray.elements)"). Enumerable().GetEnumerator(); } else { e = s.CreateQuery("select foo from foo in class NHibernate.DomainModel.Foo where foo in " + "(select elt from baz in class NHibernate.DomainModel.Baz, elt in elements(baz.FooArray))"). Enumerable().GetEnumerator(); } Assert.IsTrue(e.MoveNext()); } s.Delete(foo); s.Delete(baz); tx.Commit(); s.Close(); }
public void CachedCollection() { ISession s = OpenSession(); Baz baz = new Baz(); baz.SetDefaults(); s.Save(baz); s.Flush(); s.Close(); s = OpenSession(); baz = (Baz) s.Load(typeof(Baz), baz.Code); ((FooComponent) baz.TopComponents[0]).Count = 99; s.Flush(); s.Close(); s = OpenSession(); baz = (Baz) s.Load(typeof(Baz), baz.Code); Assert.AreEqual(99, ((FooComponent) baz.TopComponents[0]).Count); s.Delete(baz); s.Flush(); s.Close(); }
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(); }
public void ArraysOfTimes() { Baz baz; using (ISession s = OpenSession()) { baz = new Baz(); s.Save(baz); baz.SetDefaults(); s.Flush(); } using (ISession s = OpenSession()) { baz.TimeArray[2] = new DateTime(123); // H2.1: new Date(123) baz.TimeArray[3] = new DateTime(1234); // H2.1: new java.sql.Time(1234) s.Update(baz); // missing in H2.1 s.Flush(); } using (ISession s = OpenSession()) { baz = (Baz) s.Load(typeof(Baz), baz.Code); s.Delete(baz); s.Flush(); } }
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(); }
public void RemoveContains() { ISession s = OpenSession(); Baz baz = new Baz(); baz.SetDefaults(); s.Save(baz); s.Flush(); Assert.IsTrue(s.Contains(baz)); s.Evict(baz); Assert.IsFalse(s.Contains(baz), "baz should have been evicted"); Baz baz2 = (Baz) s.Load(typeof(Baz), baz.Code); Assert.IsFalse(baz == baz2, "should be different objects because Baz not contained in Session"); s.Delete(baz2); 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.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(); }
public void NewFlushing() { ISession s = OpenSession(); ITransaction txn = s.BeginTransaction(); Baz baz = new Baz(); baz.SetDefaults(); s.Save(baz); s.Flush(); baz.StringArray[0] = "a new value"; IEnumerator enumer = s.CreateQuery("from baz in class Baz").Enumerable().GetEnumerator(); // no flush Assert.IsTrue(enumer.MoveNext()); Assert.AreSame(baz, enumer.Current); enumer = IsClassicParser ? s.CreateQuery("select baz.StringArray.elements from baz in class Baz").Enumerable().GetEnumerator() : s.CreateQuery("select elements(baz.StringArray) from baz in class Baz").Enumerable().GetEnumerator(); bool found = false; while (enumer.MoveNext()) { if (enumer.Current.Equals("a new value")) { found = true; } } Assert.IsTrue(found); baz.StringArray = null; s.CreateQuery("from baz in class Baz").Enumerable(); // no flush enumer = IsClassicParser ? s.CreateQuery("select baz.StringArray.elements from baz in class Baz").Enumerable().GetEnumerator() : s.CreateQuery("select elements(baz.StringArray) from baz in class Baz").Enumerable().GetEnumerator(); Assert.IsFalse(enumer.MoveNext()); baz.StringList.Add("1E1"); enumer = s.CreateQuery("from foo in class Foo").Enumerable().GetEnumerator(); // no flush Assert.IsFalse(enumer.MoveNext()); enumer = IsClassicParser ? s.CreateQuery("select baz.StringList.elements from baz in class Baz").Enumerable().GetEnumerator() : s.CreateQuery("select elements(baz.StringList) from baz in class Baz").Enumerable().GetEnumerator(); found = false; while (enumer.MoveNext()) { if (enumer.Current.Equals("1E1")) { found = true; } } Assert.IsTrue(found); baz.StringList.Remove("1E1"); if (IsClassicParser) { s.CreateQuery("select baz.StringArray.elements from baz in class Baz").Enumerable(); //no flush } else { s.CreateQuery("select elements(baz.StringArray) from baz in class Baz").Enumerable(); //no flush } enumer = IsClassicParser ? s.CreateQuery("select baz.StringList.elements from baz in class Baz").Enumerable().GetEnumerator() : s.CreateQuery("select elements(baz.StringList) from baz in class Baz").Enumerable().GetEnumerator(); found = false; while (enumer.MoveNext()) { if (enumer.Current.Equals("1E1")) { found = true; } } Assert.IsFalse(found); IList newList = new ArrayList(); newList.Add("value"); baz.StringList = newList; s.CreateQuery("from foo in class Foo").Enumerable().GetEnumerator(); //no flush baz.StringList = null; enumer = IsClassicParser ? s.CreateQuery("select baz.StringList.elements from baz in class Baz").Enumerable().GetEnumerator() : s.CreateQuery("select elements(baz.StringList) from baz in class Baz").Enumerable().GetEnumerator(); Assert.IsFalse(enumer.MoveNext()); s.Delete(baz); txn.Commit(); s.Close(); }
public void QueryCollectionOfValues() { object gid; using (ISession s = OpenSession()) { Baz baz = new Baz(); baz.SetDefaults(); s.Save(baz); Glarch g = new Glarch(); gid = s.Save(g); if (Dialect.SupportsSubSelects) { s.CreateFilter(baz.FooArray, "where size(this.Bytes) > 0").List(); s.CreateFilter(baz.FooArray, "where 0 in elements(this.Bytes)").List(); } s.Flush(); } using (ISession s = OpenSession()) { //s.CreateQuery("from Baz baz where baz.FooSet.String = 'foo'").List(); //s.CreateQuery("from Baz baz where baz.FooArray.String = 'foo'").List(); //s.CreateQuery("from Baz baz where baz.FooSet.foo.String = 'foo'").List(); //s.CreateQuery("from Baz baz join baz.FooSet.Foo foo where foo.String = 'foo'").List(); s.CreateQuery("from Baz baz join baz.FooSet foo join foo.TheFoo.TheFoo foo2 where foo2.String = 'foo'").List(); s.CreateQuery("from Baz baz join baz.FooArray foo join foo.TheFoo.TheFoo foo2 where foo2.String = 'foo'").List(); s.CreateQuery("from Baz baz join baz.StringDateMap date where index(date) = 'foo'").List(); s.CreateQuery("from Baz baz join baz.TopGlarchez g where index(g) = 'A'").List(); s.CreateQuery("select index(g) from Baz baz join baz.TopGlarchez g").List(); Assert.AreEqual(3, s.CreateQuery("from Baz baz left join baz.StringSet").List().Count); Baz baz = (Baz) s.CreateQuery("from Baz baz join baz.StringSet str where str='foo'").List()[0]; Assert.IsFalse(NHibernateUtil.IsInitialized(baz.StringSet)); baz = (Baz) s.CreateQuery("from Baz baz left join fetch baz.StringSet").List()[0]; Assert.IsTrue(NHibernateUtil.IsInitialized(baz.StringSet)); Assert.AreEqual(1, s.CreateQuery("from Baz baz join baz.StringSet string where string='foo'").List().Count); Assert.AreEqual(1, s.CreateQuery("from Baz baz inner join baz.Components comp where comp.Name='foo'").List().Count); //IList bss = s.CreateQuery("select baz, ss from Baz baz inner join baz.StringSet ss").List(); s.CreateQuery("from Glarch g inner join g.FooComponents comp where comp.Fee is not null").List(); s.CreateQuery("from Glarch g inner join g.FooComponents comp join comp.Fee fee where fee.Count > 0").List(); s.CreateQuery("from Glarch g inner join g.FooComponents comp where comp.Fee.Count is not null").List(); s.Delete(baz); //s.delete("from Glarch g"); s.Delete(s.Get(typeof(Glarch), gid)); s.Flush(); } }
public void CollectionCache() { ISession s = OpenSession(); Baz baz = new Baz(); baz.SetDefaults(); s.Save(baz); s.Flush(); s.Close(); s = OpenSession(); s.Load(typeof(Baz), baz.Code); s.Flush(); s.Close(); s = OpenSession(); baz = (Baz) s.Load(typeof(Baz), baz.Code); s.Delete(baz); s.Flush(); s.Close(); }
public void ReuseDeletedCollection() { Baz baz, baz2; using (ISession s = OpenSession()) { baz = new Baz(); baz.SetDefaults(); s.Save(baz); s.Flush(); s.Delete(baz); baz2 = new Baz(); baz2.StringArray = new string[] {"x-y-z"}; s.Save(baz2); s.Flush(); } baz2.StringSet = baz.StringSet; baz2.StringArray = baz.StringArray; baz2.FooArray = baz.FooArray; using (ISession s = OpenSession()) { s.Update(baz2); s.Flush(); } using (ISession s = OpenSession()) { baz2 = (Baz) s.Load(typeof(Baz), baz2.Code); Assert.AreEqual(3, baz2.StringArray.Length); Assert.AreEqual(3, baz2.StringSet.Count); s.Delete(baz2); s.Flush(); } }
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 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(); }
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(); } }
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(); }