public void AddAll() { using (ISession s = OpenSession()) { Foo foo1 = new Foo(); s.Save(foo1); Foo foo2 = new Foo(); s.Save(foo2); Foo foo3 = new Foo(); s.Save(foo3); Baz baz = new Baz(); baz.FooSet = new HashedSet(); baz.FooSet.Add(foo1); s.Save(baz); Assert.AreEqual(1, baz.FooSet.Count); IList foos = new ArrayList(); foos.Add(foo2); foos.Add(foo3); baz.FooSet.AddAll(foos); Assert.AreEqual(3, baz.FooSet.Count); s.Flush(); // Clean up foreach (Foo foo in baz.FooSet) { s.Delete(foo); } s.Delete(baz); s.Flush(); } }
public void BagOneToMany() { ISession s = OpenSession(); Baz baz = new Baz(); IList list = new ArrayList(); baz.Bazez = list; list.Add(new Baz()); s.Save(baz); s.Flush(); list.Add(new Baz()); s.Flush(); list.Insert(0, new Baz()); s.Flush(); object toDelete = list[1]; list.RemoveAt(1); s.Delete(toDelete); s.Flush(); s.Delete(baz); s.Flush(); s.Close(); }
public void CascadeDeleteDetached() { Baz baz; using (ISession s = OpenSession()) { baz = new Baz(); IList list = new ArrayList(); list.Add(new Fee()); baz.Fees = list; s.Save(baz); s.Flush(); } using (ISession s = OpenSession()) { baz = (Baz) s.Get(typeof(Baz), baz.Code); } Assert.IsFalse(NHibernateUtil.IsInitialized(baz.Fees)); using (ISession s = OpenSession()) { s.Delete(baz); s.Flush(); Assert.IsFalse(s.CreateQuery("from Fee").Enumerable().GetEnumerator().MoveNext()); } using (ISession s = OpenSession()) { baz = new Baz(); IList list = new ArrayList(); list.Add(new Fee()); list.Add(new Fee()); baz.Fees = list; s.Save(baz); s.Flush(); } using (ISession s = OpenSession()) { baz = (Baz) s.Get(typeof(Baz), baz.Code); NHibernateUtil.Initialize(baz.Fees); } Assert.AreEqual(2, baz.Fees.Count); using (ISession s = OpenSession()) { s.Delete(baz); s.Flush(); Assert.IsTrue(IsEmpty(s.CreateQuery("from Fee").Enumerable())); } }
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(); }
public void OrphanDelete() { ISession s = OpenSession(); ITransaction t = s.BeginTransaction(); Baz baz = new Baz(); IDictionary bars = new Hashtable(); bars.Add(new Bar(), new object()); bars.Add(new Bar(), new object()); bars.Add(new Bar(), new object()); s.Save(baz); t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); baz = (Baz) s.Load(typeof(Baz), baz.Code); IEnumerator enumer = bars.GetEnumerator(); enumer.MoveNext(); bars.Remove(enumer.Current); s.Delete(baz); enumer.MoveNext(); bars.Remove(enumer.Current); s.Flush(); Assert.AreEqual(0, s.CreateQuery("from Bar bar").List().Count); t.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 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 CriteriaCollection() { ISession s = OpenSession(); Baz bb = (Baz) s.CreateCriteria(typeof(Baz)).UniqueResult(); Baz baz = new Baz(); s.Save(baz); s.Flush(); s.Close(); s = OpenSession(); Baz b = (Baz) s.CreateCriteria(typeof(Baz)).UniqueResult(); Assert.IsTrue(NHibernateUtil.IsInitialized(b.TopGlarchez)); Assert.AreEqual(0, b.TopGlarchez.Count); s.Delete(b); s.Flush(); s.CreateCriteria(typeof(Baz)) .CreateCriteria("TopFoos") .Add(Expression.IsNotNull("id")) .List(); s.CreateCriteria(typeof(Baz)) .CreateCriteria("Foo") .CreateCriteria("Component.Glarch") .CreateCriteria("ProxySet") .Add(Expression.IsNotNull("id")) .List(); s.Close(); }
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(); }
public void CollectionWhere() { Foo foo1 = new Foo(); Foo foo2 = new Foo(); Baz baz = new Baz(); Foo[] arr = new Foo[10]; arr[0] = foo1; arr[9] = foo2; ISession s = OpenSession(); s.Save(foo1); s.Save(foo2); baz.FooArray = arr; s.Save(baz); s.Flush(); s.Close(); s = OpenSession(); baz = (Baz) s.Load(typeof(Baz), baz.Code); Assert.AreEqual(1, baz.FooArray.Length); Assert.AreEqual(1, s.CreateQuery("from Baz baz join baz.FooArray foo").List().Count); Assert.AreEqual(2, s.CreateQuery("from Foo foo").List().Count); Assert.AreEqual(1, s.CreateFilter(baz.FooArray, "").List().Count); s.Delete("from Foo foo"); s.Delete(baz); IDbCommand deleteCmd = s.Connection.CreateCommand(); deleteCmd.CommandText = "delete from FooArray where id_='" + baz.Code + "' and i>=8"; deleteCmd.CommandType = CommandType.Text; int rows = deleteCmd.ExecuteNonQuery(); Assert.AreEqual(1, rows); s.Flush(); s.Close(); }
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(); }
public void SaveAddDelete() { ISession s = OpenSession(); Baz baz = new Baz(); ISet bars = new HashedSet(); baz.CascadingBars = bars; s.Save(baz); s.Flush(); baz.CascadingBars.Add(new Bar()); s.Delete(baz); s.Flush(); s.Close(); }
public void IdBag() { ISession s = OpenSession(); Baz baz = new Baz(); s.Save(baz); IList l = new ArrayList(); IList l2 = new ArrayList(); baz.IdFooBag = l; baz.ByteBag = l2; l.Add(new Foo()); l.Add(new Bar()); byte[] bytes = GetBytes("ffo"); l2.Add(bytes); l2.Add(GetBytes("foo")); s.Flush(); l.Add(new Foo()); l.Add(new Bar()); l2.Add(GetBytes("bar")); s.Flush(); object removedObject = l[3]; l.RemoveAt(3); s.Delete(removedObject); bytes[1] = Convert.ToByte('o'); s.Flush(); s.Close(); s = OpenSession(); baz = (Baz) s.Load(typeof(Baz), baz.Code); Assert.AreEqual(3, baz.IdFooBag.Count); Assert.AreEqual(3, baz.ByteBag.Count); bytes = GetBytes("foobar"); foreach (object obj in baz.IdFooBag) { s.Delete(obj); } baz.IdFooBag = null; baz.ByteBag.Add(bytes); baz.ByteBag.Add(bytes); s.Flush(); s.Close(); s = OpenSession(); baz = (Baz) s.Load(typeof(Baz), baz.Code); Assert.AreEqual(0, baz.IdFooBag.Count); Assert.AreEqual(5, baz.ByteBag.Count); s.Delete(baz); s.Flush(); s.Close(); }
public void ManyToManyBag() { ISession s = OpenSession(); Baz baz = new Baz(); object id = s.Save(baz); s.Flush(); s.Close(); s = OpenSession(); baz = (Baz) s.Load(typeof(Baz), id); baz.FooBag.Add(new Foo()); s.Flush(); s.Close(); s = OpenSession(); baz = (Baz) s.Load(typeof(Baz), id); Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooBag)); Assert.AreEqual(1, baz.FooBag.Count); Assert.IsTrue(NHibernateUtil.IsInitialized(baz.FooBag[0])); s.Delete(baz); s.Flush(); s.Close(); }
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(); }
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 Reachability() { // first for unkeyed collections ISession s = OpenSession(); Baz baz1 = new Baz(); s.Save(baz1); Baz baz2 = new Baz(); s.Save(baz2); baz1.IntArray = new int[] {1, 2, 3, 4}; baz1.FooSet = new HashedSet(); Foo foo = new Foo(); s.Save(foo); baz1.FooSet.Add(foo); s.Flush(); s.Close(); s = OpenSession(); baz2 = (Baz) s.Load(typeof(Baz), baz2.Code); baz1 = (Baz) s.Load(typeof(Baz), baz1.Code); baz2.FooSet = baz1.FooSet; baz1.FooSet = null; baz2.IntArray = baz1.IntArray; baz1.IntArray = null; s.Flush(); s.Close(); s = OpenSession(); baz2 = (Baz) s.Load(typeof(Baz), baz2.Code); baz1 = (Baz) s.Load(typeof(Baz), baz1.Code); Assert.AreEqual(4, baz2.IntArray.Length, "unkeyed reachability - baz2.IntArray"); Assert.AreEqual(1, baz2.FooSet.Count, "unkeyed reachability - baz2.FooSet"); Assert.AreEqual(0, baz1.IntArray.Length, "unkeyed reachability - baz1.IntArray"); Assert.AreEqual(0, baz1.FooSet.Count, "unkeyed reachability - baz1.FooSet"); foreach (object obj in baz2.FooSet) { s.Delete((FooProxy) obj); } s.Delete(baz1); s.Delete(baz2); s.Flush(); s.Close(); // now for collections of collections s = OpenSession(); baz1 = new Baz(); s.Save(baz1); baz2 = new Baz(); s.Save(baz2); s.Flush(); s.Close(); s = OpenSession(); baz2 = (Baz) s.Load(typeof(Baz), baz2.Code); baz1 = (Baz) s.Load(typeof(Baz), baz1.Code); s.Flush(); s.Close(); s = OpenSession(); baz2 = (Baz) s.Load(typeof(Baz), baz2.Code); s.Flush(); s.Close(); s = OpenSession(); baz2 = (Baz) s.Load(typeof(Baz), baz2.Code); baz1 = (Baz) s.Load(typeof(Baz), baz1.Code); s.Delete(baz1); s.Delete(baz2); s.Flush(); s.Close(); // now for keyed collections s = OpenSession(); baz1 = new Baz(); s.Save(baz1); baz2 = new Baz(); s.Save(baz2); Foo foo1 = new Foo(); Foo foo2 = new Foo(); s.Save(foo1); s.Save(foo2); baz1.FooArray = new Foo[] {foo1, null, foo2}; baz1.StringDateMap = new Hashtable(); baz1.StringDateMap["today"] = DateTime.Today; baz1.StringDateMap["tomm"] = new DateTime(DateTime.Today.Ticks + (new TimeSpan(1, 0, 0, 0, 0)).Ticks); s.Flush(); s.Close(); s = OpenSession(); baz2 = (Baz) s.Load(typeof(Baz), baz2.Code); baz1 = (Baz) s.Load(typeof(Baz), baz1.Code); baz2.FooArray = baz1.FooArray; baz1.FooArray = null; baz2.StringDateMap = baz1.StringDateMap; baz1.StringDateMap = null; s.Flush(); s.Close(); s = OpenSession(); baz2 = (Baz) s.Load(typeof(Baz), baz2.Code); baz1 = (Baz) s.Load(typeof(Baz), baz1.Code); Assert.AreEqual(2, baz2.StringDateMap.Count, "baz2.StringDateMap count - reachability"); Assert.AreEqual(3, baz2.FooArray.Length, "baz2.FooArray length - reachability"); Assert.AreEqual(0, baz1.StringDateMap.Count, "baz1.StringDateMap count - reachability"); Assert.AreEqual(0, baz1.FooArray.Length, "baz1.FooArray length - reachability"); Assert.IsNull(baz2.FooArray[1], "null element"); Assert.IsNotNull(baz2.StringDateMap["today"], "today non-null element"); Assert.IsNotNull(baz2.StringDateMap["tomm"], "tomm non-null element"); Assert.IsNull(baz2.StringDateMap["foo"], "foo is null element"); s.Delete(baz2.FooArray[0]); s.Delete(baz2.FooArray[2]); s.Delete(baz1); s.Delete(baz2); s.Flush(); s.Close(); }
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 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 DereferenceLazyCollection() { string fooKey; string bazCode; using (ISession s = OpenSession()) { Baz baz = new Baz(); baz.FooSet = new HashedSet(); Foo foo = new Foo(); baz.FooSet.Add(foo); s.Save(foo); s.Save(baz); foo.Bytes = GetBytes("foobar"); s.Flush(); fooKey = foo.Key; bazCode = baz.Code; } using (ISession s = OpenSession()) { Foo foo = (Foo) s.Get(typeof(Foo), fooKey); Assert.IsTrue(NHibernateUtil.IsInitialized(foo.Bytes)); // H2.1 has 6 here, but we are using Unicode Assert.AreEqual(12, foo.Bytes.Length); Baz baz = (Baz) s.Get(typeof(Baz), bazCode); Assert.AreEqual(1, baz.FooSet.Count); s.Flush(); } sessions.EvictCollection("NHibernate.DomainModel.Baz.FooSet"); using (ISession s = OpenSession()) { Baz baz = (Baz) s.Get(typeof(Baz), bazCode); Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet)); baz.FooSet = null; s.Flush(); } using (ISession s = OpenSession()) { Foo foo = (Foo) s.Get(typeof(Foo), fooKey); Assert.AreEqual(12, foo.Bytes.Length); Baz baz = (Baz) s.Get(typeof(Baz), bazCode); Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet)); Assert.AreEqual(0, baz.FooSet.Count); s.Delete(baz); s.Delete(foo); 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 CascadeSave() { ISession s = OpenSession(); ITransaction t = s.BeginTransaction(); Baz baz = new Baz(); IList list = new ArrayList(); list.Add(new Fee()); list.Add(new Fee()); baz.Fees = list; s.Save(baz); t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); baz = (Baz) s.Load(typeof(Baz), baz.Code); Assert.AreEqual(2, baz.Fees.Count); s.Delete(baz); Assert.IsTrue(IsEmpty(s.CreateQuery("from fee in class Fee").Enumerable())); t.Commit(); s.Close(); }
public void AutosaveChildren() { ISession s = OpenSession(); ITransaction t = s.BeginTransaction(); Baz baz = new Baz(); ISet bars = new HashedSet(); baz.CascadingBars = bars; s.Save(baz); t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); baz = (Baz) s.Load(typeof(Baz), baz.Code); baz.CascadingBars.Add(new Bar()); baz.CascadingBars.Add(new Bar()); t.Commit(); s.Close(); s = OpenSession(); t = s.BeginTransaction(); baz = (Baz) s.Load(typeof(Baz), baz.Code); Assert.AreEqual(2, baz.CascadingBars.Count); IEnumerator enumer = baz.CascadingBars.GetEnumerator(); Assert.IsTrue(enumer.MoveNext()); Assert.IsNotNull(enumer.Current); baz.CascadingBars.Clear(); // test all-delete-orphan s.Flush(); Assert.AreEqual(0, s.CreateQuery("from Bar bar").List().Count); s.Delete(baz); t.Commit(); 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 TransientOrphanDelete() { ISession s = OpenSession(); ITransaction t = s.BeginTransaction(); Baz baz = new Baz(); ISet bars = new HashedSet(); baz.CascadingBars = bars; bars.Add(new Bar()); bars.Add(new Bar()); bars.Add(new Bar()); IList foos = new ArrayList(); foos.Add(new Foo()); foos.Add(new Foo()); baz.FooBag = foos; s.Save(baz); IEnumerator enumer = new JoinedEnumerable(new IEnumerable[] {foos, bars}).GetEnumerator(); while (enumer.MoveNext()) { FooComponent cmp = ((Foo) enumer.Current).Component; s.Delete(cmp.Glarch); cmp.Glarch = null; } t.Commit(); s.Close(); enumer = bars.GetEnumerator(); enumer.MoveNext(); bars.Remove(enumer.Current); foos.RemoveAt(1); s = OpenSession(); t = s.BeginTransaction(); s.Update(baz); Assert.AreEqual(2, s.CreateQuery("from Bar bar").List().Count); Assert.AreEqual(3, s.CreateQuery("from Foo foo").List().Count); t.Commit(); s.Close(); foos.RemoveAt(0); s = OpenSession(); t = s.BeginTransaction(); s.Update(baz); enumer = bars.GetEnumerator(); enumer.MoveNext(); bars.Remove(enumer.Current); s.Delete(baz); s.Flush(); Assert.AreEqual(0, s.CreateQuery("from Foo foo").List().Count); 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 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 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 ForeignKeys() { Baz baz; using (ISession s = OpenSession()) { baz = new Baz(); Foo foo = new Foo(); IList bag = new ArrayList(); bag.Add(foo); baz.IdFooBag = bag; baz.Foo = foo; s.Save(baz); s.Flush(); } using (ISession s = OpenSession()) { baz = (Baz) s.Load(typeof(Baz), baz.Code); s.Delete(baz); s.Flush(); } }
public void MoveLazyCollection() { string fooKey, bazCode, baz2Code; using (ISession s = OpenSession()) { Baz baz = new Baz(); Baz baz2 = new Baz(); baz.FooSet = new HashedSet(); Foo foo = new Foo(); baz.FooSet.Add(foo); s.Save(foo); s.Save(baz); s.Save(baz2); foo.Bytes = GetBytes("foobar"); s.Flush(); fooKey = foo.Key; bazCode = baz.Code; baz2Code = baz2.Code; } using (ISession s = OpenSession()) { Foo foo = (Foo) s.Get(typeof(Foo), fooKey); Assert.IsTrue(NHibernateUtil.IsInitialized(foo.Bytes)); Assert.AreEqual(12, foo.Bytes.Length); Baz baz = (Baz) s.Get(typeof(Baz), bazCode); Assert.AreEqual(1, baz.FooSet.Count); s.Flush(); } sessions.EvictCollection("NHibernate.DomainModel.Baz.FooSet"); using (ISession s = OpenSession()) { Baz baz = (Baz) s.Get(typeof(Baz), bazCode); Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet)); Baz baz2 = (Baz) s.Get(typeof(Baz), baz2Code); baz2.FooSet = baz.FooSet; baz.FooSet = null; Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.FooSet)); s.Flush(); } using (ISession s = OpenSession()) { Foo foo = (Foo) s.Get(typeof(Foo), fooKey); Assert.AreEqual(12, foo.Bytes.Length); Baz baz = (Baz) s.Get(typeof(Baz), bazCode); Baz baz2 = (Baz) s.Get(typeof(Baz), baz2Code); Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet)); Assert.AreEqual(0, baz.FooSet.Count); Assert.IsTrue( NHibernateUtil.IsInitialized( baz2.FooSet ) ); //FooSet has batching enabled Assert.AreEqual(1, baz2.FooSet.Count); s.Delete(baz); s.Delete(baz2); s.Delete(foo); s.Flush(); } }