public void ObjectType() { object gid; using (ISession s = OpenSession()) { GlarchProxy g = new Glarch(); Foo foo = new Foo(); g.Any = foo; gid = s.Save(g); s.Save(foo); s.Flush(); } using (ISession s = OpenSession()) { GlarchProxy g = (GlarchProxy) s.Load(typeof(Glarch), gid); Assert.IsNotNull(g.Any); Assert.IsTrue(g.Any is FooProxy); s.Delete(g.Any); s.Delete(g); s.Flush(); } }
public void ForCertain() { Glarch g = new Glarch(); Glarch g2 = new Glarch(); IList strings = new ArrayList(); strings.Add("foo"); g2.Strings = strings; object gid, g2id; using (ISession s = OpenSession()) { using (ITransaction t = s.BeginTransaction()) { gid = s.Save(g); g2id = s.Save(g2); t.Commit(); // Versions are initialized to 1 in NH. Assert.AreEqual(1, g.Version); Assert.AreEqual(1, g2.Version); } } using (ISession s = OpenSession()) { using (ITransaction t = s.BeginTransaction()) { g = (Glarch) s.Get(typeof(Glarch), gid); g2 = (Glarch) s.Get(typeof(Glarch), g2id); Assert.AreEqual(1, g2.Strings.Count); s.Delete(g); s.Delete(g2); t.Commit(); } } }
public void NoForeignKeyViolations() { ISession s = OpenSession(); Glarch g1 = new Glarch(); Glarch g2 = new Glarch(); g1.Next = g2; g2.Next = g1; s.Save(g1); s.Save(g2); s.Flush(); s.Close(); s = OpenSession(); IList l = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch where g.Next is not null").List(); s.Delete(l[0]); s.Delete(l[1]); s.Flush(); s.Close(); }
public void ProxyArray() { ISession s = OpenSession(); GlarchProxy g = new Glarch(); Glarch g1 = new Glarch(); Glarch g2 = new Glarch(); g.ProxyArray = new GlarchProxy[] {g1, g2}; Glarch g3 = new Glarch(); s.Save(g3); g2.ProxyArray = new GlarchProxy[] {null, g3, g}; ISet hashset = new HashedSet(); hashset.Add(g1); hashset.Add(g2); g.ProxySet = hashset; s.Save(g); s.Save(g1); s.Save(g2); object id = s.GetIdentifier(g); s.Flush(); s.Close(); s = OpenSession(); g = (GlarchProxy) s.Load(typeof(Glarch), id); Assert.AreEqual(2, g.ProxyArray.Length, "array of proxies"); Assert.IsNotNull(g.ProxyArray[0], "array of proxies"); Assert.IsNull(g.ProxyArray[1].ProxyArray[0], "deferred load test"); Assert.AreEqual(g, g.ProxyArray[1].ProxyArray[2], "deferred load test"); Assert.AreEqual(2, g.ProxySet.Count, "set of proxies"); IEnumerator enumer = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch").Enumerable().GetEnumerator(); while (enumer.MoveNext()) { s.Delete(enumer.Current); } s.Flush(); s.Disconnect(); // serialize the session. Stream stream = new MemoryStream(); IFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, s); // close the original session s.Close(); // deserialize the session stream.Position = 0; s = (ISession) formatter.Deserialize(stream); stream.Close(); s.Close(); }
public void VersionedCollections() { ISession s = OpenSession(); GlarchProxy g = new Glarch(); s.Save(g); g.ProxyArray = new GlarchProxy[] {g}; string gid = (string) s.GetIdentifier(g); ArrayList list = new ArrayList(); list.Add("foo"); g.Strings = list; // <sets> in h2.0.3 ISet hashset = new HashedSet(); hashset.Add(g); g.ProxySet = hashset; s.Flush(); s.Close(); s = OpenSession(); g = (GlarchProxy) s.Load(typeof(Glarch), gid); Assert.AreEqual(1, g.Strings.Count); Assert.AreEqual(1, g.ProxyArray.Length); Assert.AreEqual(1, g.ProxySet.Count); Assert.AreEqual(2, g.Version, "version collection before"); s.Flush(); s.Close(); s = OpenSession(); g = (GlarchProxy) s.Load(typeof(Glarch), gid); Assert.AreEqual("foo", g.Strings[0]); Assert.AreSame(g, g.ProxyArray[0]); IEnumerator enumer = g.ProxySet.GetEnumerator(); enumer.MoveNext(); Assert.AreSame(g, enumer.Current); Assert.AreEqual(2, g.Version, "versioned collection before"); s.Flush(); s.Close(); s = OpenSession(); g = (GlarchProxy) s.Load(typeof(Glarch), gid); Assert.AreEqual(2, g.Version, "versioned collection before"); g.Strings.Add("bar"); s.Flush(); s.Close(); s = OpenSession(); g = (GlarchProxy) s.Load(typeof(Glarch), gid); Assert.AreEqual(3, g.Version, "versioned collection after"); Assert.AreEqual(2, g.Strings.Count, "versioned collection after"); g.ProxyArray = null; s.Flush(); s.Close(); s = OpenSession(); g = (GlarchProxy) s.Load(typeof(Glarch), gid); Assert.AreEqual(4, g.Version, "versioned collection after"); Assert.AreEqual(0, g.ProxyArray.Length, "version collection after"); g.FooComponents = new ArrayList(); g.ProxyArray = null; s.Flush(); s.Close(); s = OpenSession(); g = (GlarchProxy) s.Load(typeof(Glarch), gid); Assert.AreEqual(5, g.Version, "versioned collection after"); s.Delete(g); s.Flush(); s.Close(); }
public void RecursiveLoad() { // Non polymorphisc class (there is an implementation optimization // being tested here) - from h2.0.3 - what does that mean? ISession s = OpenSession(); ITransaction txn = s.BeginTransaction(); GlarchProxy last = new Glarch(); s.Save(last); last.Order = 0; for (int i = 0; i < 5; i++) { GlarchProxy next = new Glarch(); s.Save(next); last.Next = next; last = next; last.Order = (short) (i + 1); } IEnumerator enumer = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch").Enumerable().GetEnumerator(); while (enumer.MoveNext()) { object objTemp = enumer.Current; } IList list = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch").List(); Assert.AreEqual(6, list.Count, "recursive find"); txn.Commit(); s.Close(); s = OpenSession(); txn = s.BeginTransaction(); list = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch").List(); Assert.AreEqual(6, list.Count, "recursive iter"); list = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch where g.Next is not null").List(); Assert.AreEqual(5, list.Count, "exclude the null next"); txn.Commit(); s.Close(); s = OpenSession(); txn = s.BeginTransaction(); enumer = s.CreateQuery("from g in class NHibernate.DomainModel.Glarch order by g.Order asc").Enumerable().GetEnumerator(); while (enumer.MoveNext()) { GlarchProxy g = (GlarchProxy) enumer.Current; Assert.IsNotNull(g, "not null"); // no equiv in .net - so ran a delete query // iter.remove(); } s.Delete("from NHibernate.DomainModel.Glarch as g"); txn.Commit(); s.Close(); // same thing bug using polymorphic class (no optimization possible) s = OpenSession(); txn = s.BeginTransaction(); FooProxy flast = new Bar(); s.Save(flast); for (int i = 0; i < 5; i++) { FooProxy foo = new Bar(); s.Save(foo); flast.TheFoo = foo; flast = flast.TheFoo; flast.String = "foo" + (i + 1); } enumer = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").Enumerable().GetEnumerator(); while (enumer.MoveNext()) { object objTemp = enumer.Current; } list = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List(); Assert.AreEqual(6, list.Count, "recursive find"); txn.Commit(); s.Close(); s = OpenSession(); txn = s.BeginTransaction(); list = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo").List(); Assert.AreEqual(6, list.Count, "recursive iter"); enumer = list.GetEnumerator(); while (enumer.MoveNext()) { Assert.IsTrue(enumer.Current is BarProxy, "polymorphic recursive load"); } txn.Commit(); s.Close(); s = OpenSession(); txn = s.BeginTransaction(); enumer = s.CreateQuery("from foo in class NHibernate.DomainModel.Foo order by foo.String asc").Enumerable().GetEnumerator(); string currentString = String.Empty; while (enumer.MoveNext()) { BarProxy bar = (BarProxy) enumer.Current; string theString = bar.String; Assert.IsNotNull(bar, "not null"); if (currentString != String.Empty) { Assert.IsTrue(theString.CompareTo(currentString) >= 0, "not in asc order"); } currentString = theString; // no equiv in .net - so made a hql delete // iter.remove(); } s.Delete("from NHibernate.DomainModel.Foo as foo"); txn.Commit(); s.Close(); }
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 Versioning() { GlarchProxy g = new Glarch(); GlarchProxy g2 = new Glarch(); object gid, g2id; using (ISession s = OpenSession()) using (ITransaction txn = s.BeginTransaction()) { s.Save(g); s.Save(g2); gid = s.GetIdentifier(g); g2id = s.GetIdentifier(g2); g.Name = "glarch"; txn.Commit(); } sessions.Evict(typeof(Glarch)); using (ISession s = OpenSession()) using (ITransaction txn = s.BeginTransaction()) { g = (GlarchProxy) s.Load(typeof(Glarch), gid); s.Lock(g, LockMode.Upgrade); g2 = (GlarchProxy) s.Load(typeof(Glarch), g2id); // Versions are initialized to 1 in NH (not to 0 like in Hibernate) Assert.AreEqual(2, g.Version, "version"); Assert.AreEqual(2, g.DerivedVersion, "version"); Assert.AreEqual(1, g2.Version, "version"); g.Name = "foo"; Assert.IsTrue( s.CreateQuery("from g in class Glarch where g.Version=3").List().Count == 1, "find by version" ); g.Name = "bar"; txn.Commit(); } sessions.Evict(typeof(Glarch)); using (ISession s = OpenSession()) using (ITransaction txn = s.BeginTransaction()) { g = (GlarchProxy) s.Load(typeof(Glarch), gid); g2 = (GlarchProxy) s.Load(typeof(Glarch), g2id); Assert.AreEqual(4, g.Version, "version"); Assert.AreEqual(4, g.DerivedVersion, "version"); Assert.AreEqual(1, g2.Version, "version"); g.Next = null; g2.Next = g; s.Delete(g2); s.Delete(g); txn.Commit(); } }
public void Custom() { GlarchProxy g = new Glarch(); Multiplicity m = new Multiplicity(); m.count = 12; m.glarch = (Glarch) g; g.Multiple = m; ISession s = OpenSession(); object gid = s.Save(g); s.Flush(); s.Close(); s = OpenSession(); g = (Glarch) s.CreateQuery("from Glarch g where g.Multiple.glarch=g and g.Multiple.count=12").List()[0]; Assert.IsNotNull(g.Multiple); Assert.AreEqual(12, g.Multiple.count); Assert.AreSame(g, g.Multiple.glarch); s.Flush(); s.Close(); s = OpenSession(); g = (GlarchProxy) s.Load(typeof(Glarch), gid); Assert.IsNotNull(g.Multiple); Assert.AreEqual(12, g.Multiple.count); Assert.AreSame(g, g.Multiple.glarch); s.Delete(g); s.Flush(); s.Close(); }
public void Dyna() { ISession s = OpenSession(); GlarchProxy g = new Glarch(); g.Name = "G"; object id = s.Save(g); s.Flush(); s.Close(); s = OpenSession(); g = (GlarchProxy) s.Load(typeof(Glarch), id); Assert.AreEqual("G", g.Name); Assert.AreEqual("foo", g.DynaBean["foo"]); Assert.AreEqual(66, g.DynaBean["bar"]); Assert.IsFalse(g is Glarch); g.DynaBean["foo"] = "bar"; s.Flush(); s.Close(); s = OpenSession(); g = (GlarchProxy) s.Load(typeof(Glarch), id); Assert.AreEqual("bar", g.DynaBean["foo"]); Assert.AreEqual(66, g.DynaBean["bar"]); g.DynaBean = null; s.Flush(); s.Close(); s = OpenSession(); g = (GlarchProxy) s.Load(typeof(Glarch), id); Assert.IsNull(g.DynaBean); s.Delete(g); s.Flush(); s.Close(); }
public void ForceOuterJoin() { if (sessions.Settings.IsOuterJoinFetchEnabled == false) { // don't bother to run the test if we can't test it return; } ISession s = OpenSession(); Glarch g = new Glarch(); FooComponent fc = new FooComponent(); fc.Glarch = g; FooProxy f = new Foo(); FooProxy f2 = new Foo(); f.Component = fc; f.TheFoo = f2; s.Save(f2); object id = s.Save(f); object gid = s.GetIdentifier(f.Component.Glarch); s.Flush(); s.Close(); s = OpenSession(); f = (FooProxy) s.Load(typeof(Foo), id); Assert.IsFalse(NHibernateUtil.IsInitialized(f)); Assert.IsTrue(NHibernateUtil.IsInitialized(f.Component.Glarch)); //outer-join="true" Assert.IsFalse(NHibernateUtil.IsInitialized(f.TheFoo)); //outer-join="auto" Assert.AreEqual(gid, s.GetIdentifier(f.Component.Glarch)); s.Delete(f); s.Delete(f.TheFoo); s.Delete(f.Component.Glarch); s.Flush(); 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 NonlazyCollections() { object glarchId; using (ISession s = OpenSession()) { Glarch glarch1 = new Glarch(); glarch1.ProxySet = new ListSet(); Glarch glarch2 = new Glarch(); glarch1.ProxySet.Add(glarch1); s.Save(glarch2); glarchId = s.Save(glarch1); s.Flush(); } Glarch loadedGlarch; using (ISession s = OpenSession()) { loadedGlarch = (Glarch) s.Get(typeof(Glarch), glarchId); Assert.IsTrue(NHibernateUtil.IsInitialized(loadedGlarch.ProxySet)); } // ProxySet is a non-lazy collection, so this should work outside // a session. Assert.AreEqual(1, loadedGlarch.ProxySet.Count); using (ISession s = OpenSession()) { s.Delete("from Glarch"); s.Flush(); } }
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 Versioning() { GlarchProxy g = new Glarch(); GlarchProxy g2 = new Glarch(); object gid, g2id; using( ISession s = OpenSession() ) { s.Save( g ); s.Save( g2 ); gid = s.GetIdentifier( g ); g2id = s.GetIdentifier( g2 ); g.Name = "glarch"; s.Flush(); } sessions.Evict( typeof( Glarch ) ); using( ISession s = OpenSession() ) { g = ( GlarchProxy ) s.Load( typeof( Glarch ), gid ); s.Lock( g, LockMode.Upgrade ); g2 = ( GlarchProxy ) s.Load( typeof( Glarch ), g2id ); Assert.IsTrue( g.Version == 1, "version" ); Assert.IsTrue( g.DerivedVersion == 1, "version" ); Assert.IsTrue( g2.Version == 0, "version" ); g.Name = "foo"; Assert.IsTrue( s.Find( "from g in class Glarch where g.Version=2" ).Count == 1, "find by version" ); g.Name = "bar"; s.Flush(); } sessions.Evict( typeof( Glarch ) ); using( ISession s = OpenSession() ) { g = ( GlarchProxy ) s.Load( typeof( Glarch ), gid ); g2 = ( GlarchProxy ) s.Load( typeof( Glarch ), g2id ); Assert.IsTrue( g.Version == 3, "version" ); Assert.IsTrue( g.DerivedVersion == 3, "version" ); Assert.IsTrue( g2.Version == 0, "version" ); g.Next = null; g2.Next = g; s.Delete( g2 ); s.Delete( g ); s.Flush(); } }