Exemplo n.º 1
0
        public void UpdateLazyCollections()
        {
            // if (dialect is HSQLDialect) return;
            ISession s  = OpenSession();
            Master   m  = new Master();
            Detail   d1 = new Detail();
            Detail   d2 = new Detail();

            d2.X = 14;
            object mid = s.Save(m);

            // s.Flush();  commented out in h2.0.3 also
            d1.Master = m;
            d2.Master = m;
            m.AddDetail(d1);
            m.AddDetail(d2);
            if ((Dialect is SybaseDialect) || (Dialect is MsSql2000Dialect))
            {
                s.Save(d1);
                s.Save(d2);
            }
            else
            {
                s.Save(d1, 666L);
                s.Save(d2, 667L);
            }
            s.Flush();
            s.Close();

            s = OpenSession();
            m = (Master)s.Load(typeof(Master), mid);
            s.Close();
            m.Name = "New Name";
            s      = OpenSession();
            s.Update(m, mid);
            IEnumerator enumer = m.Details.GetEnumerator();
            int         i      = 0;

            while (enumer.MoveNext())
            {
                Assert.IsNotNull(enumer.Current);
                i++;
            }
            Assert.AreEqual(2, i);

            enumer = m.Details.GetEnumerator();
            while (enumer.MoveNext())
            {
                s.Delete(enumer.Current);
            }
            s.Delete(m);
            s.Flush();
            s.Close();
        }
Exemplo n.º 2
0
        public async Task UpdateLazyCollectionsAsync()
        {
            // if (dialect is HSQLDialect) return;
            ISession s  = OpenSession();
            Master   m  = new Master();
            Detail   d1 = new Detail();
            Detail   d2 = new Detail();

            d2.X = 14;
            object mid = await(s.SaveAsync(m));

            // s.Flush();  commented out in h2.0.3 also
            d1.Master = m;
            d2.Master = m;
            m.AddDetail(d1);
            m.AddDetail(d2);
            if (Dialect is MsSql2000Dialect)
            {
                await(s.SaveAsync(d1));
                await(s.SaveAsync(d2));
            }
            else
            {
                await(s.SaveAsync(d1, 666L));
                await(s.SaveAsync(d2, 667L));
            }
            await(s.FlushAsync());
            s.Close();

            s = OpenSession();
            m = (Master)await(s.LoadAsync(typeof(Master), mid));
            s.Close();
            m.Name = "New Name";
            s      = OpenSession();
            await(s.UpdateAsync(m, mid));
            IEnumerator enumer = m.Details.GetEnumerator();
            int         i      = 0;

            while (enumer.MoveNext())
            {
                Assert.IsNotNull(enumer.Current);
                i++;
            }
            Assert.AreEqual(2, i);

            enumer = m.Details.GetEnumerator();
            while (enumer.MoveNext())
            {
                await(s.DeleteAsync(enumer.Current));
            }
            await(s.DeleteAsync(m));
            await(s.FlushAsync());
            s.Close();
        }
Exemplo n.º 3
0
        public void MultiLevelCascade()
        {
            //if( dialect is Dialect.HSQLDialect ) return;

            object mid;
            object m0id;

            using (ISession s = OpenSession())
            {
                Detail    detail    = new Detail();
                SubDetail subdetail = new SubDetail();
                Master    m         = new Master();
                Master    m0        = new Master();
                m0id = s.Save(m0);
                m0.AddDetail(detail);
                detail.Master = m0;
                m.MoreDetails.Add(detail);
                detail.SubDetails = new HashedSet();
                detail.SubDetails.Add(subdetail);
                mid = s.Save(m);
                s.Flush();
            }

            using (ISession s = OpenSession())
            {
                Master      m      = (Master)s.Load(typeof(Master), mid);
                IEnumerator enumer = m.MoreDetails.GetEnumerator();
                enumer.MoveNext();
                Assert.IsTrue(((Detail)enumer.Current).SubDetails.Count != 0);
                s.Delete(m);
                s.Delete(s.Load(typeof(Master), m0id));
                s.Flush();
            }
        }
Exemplo n.º 4
0
        public async Task MultiLevelCascadeAsync()
        {
            //if( dialect is Dialect.HSQLDialect ) return;

            object mid;
            object m0id;

            using (ISession s = OpenSession())
            {
                Detail    detail    = new Detail();
                SubDetail subdetail = new SubDetail();
                Master    m         = new Master();
                Master    m0        = new Master();
                m0id = await(s.SaveAsync(m0));
                m0.AddDetail(detail);
                detail.Master = m0;
                m.MoreDetails.Add(detail);
                detail.SubDetails = new HashSet <SubDetail> {
                    subdetail
                };
                mid = await(s.SaveAsync(m));
                await(s.FlushAsync());
            }

            using (ISession s = OpenSession())
            {
                Master      m      = (Master)await(s.LoadAsync(typeof(Master), mid));
                IEnumerator enumer = m.MoreDetails.GetEnumerator();
                enumer.MoveNext();
                Assert.IsTrue(((Detail)enumer.Current).SubDetails.Count != 0);
                await(s.DeleteAsync(m));
                await(s.DeleteAsync(await(s.LoadAsync(typeof(Master), m0id))));
                await(s.FlushAsync());
            }
        }
Exemplo n.º 5
0
        public void Cascading()
        {
            //HSQLDialect return;

            ISession s  = OpenSession();
            Detail   d1 = new Detail();
            Detail   d2 = new Detail();

            d2.I = 22;
            Master m    = new Master();
            Master m0   = new Master();
            object m0id = s.Save(m0);

            m0.AddDetail(d1);
            m0.AddDetail(d2);
            d1.Master = m0;
            d2.Master = m0;
            m.MoreDetails.Add(d1);
            m.MoreDetails.Add(d2);
            object mid = s.Save(m);

            s.Flush();
            s.Close();

            s = OpenSession();
            m = (Master)s.Load(typeof(Master), mid);
            Assert.AreEqual(2, m.MoreDetails.Count, "cascade save");
            IEnumerator enumer = m.MoreDetails.GetEnumerator();

            enumer.MoveNext();
            Assert.AreEqual(2, ((Detail)enumer.Current).Master.Details.Count, "cascade save");

            s.Delete(m);
            s.Delete(s.Load(typeof(Master), m0id));
            s.Flush();
            s.Close();
        }
        private void CreateMaster()
        {
            Master m = new Master();

            m.Name = "Pepe";
            for (int i = 1; i <= 3; i++)
            {
                Detail d = new Detail("d" + i, m);
                m.AddDetail(d);
            }

            using (ISession s = OpenSession())
                using (ITransaction t = s.BeginTransaction())
                {
                    s.Save(m);
                    savedId = m.Id;
                    t.Commit();
                }
        }
Exemplo n.º 7
0
        public void Serialization()
        {
            ISession s   = OpenSession();
            Master   m   = new Master();
            Detail   d1  = new Detail();
            Detail   d2  = new Detail();
            object   mid = s.Save(m);

            d1.Master = (m);
            d2.Master = (m);
            m.AddDetail(d1);
            m.AddDetail(d2);
            if ((Dialect is SybaseDialect) || (Dialect is MsSql2000Dialect))
            {
                s.Save(d1);
            }
            else
            {
                s.Save(d1, 666L);
            }
            s.Flush();
            s.Disconnect();
            MemoryStream    stream = new MemoryStream();
            BinaryFormatter f      = new BinaryFormatter();

            f.Serialize(stream, s);
            stream.Position = 0;
            Console.WriteLine(stream.Length);

            s = (ISession)f.Deserialize(stream);
            stream.Close();

            s.Reconnect();
            Master m2 = (Master)s.Load(typeof(Master), mid);

            Assert.IsTrue(m2.Details.Count == 2, "serialized state");
            foreach (Detail d in m2.Details)
            {
                Assert.IsTrue(d.Master == m2, "deserialization");
                try
                {
                    s.GetIdentifier(d);
                    s.Delete(d);
                }
                catch (Exception e)
                {
                    Assert.IsNotNull(e);                     //getting ride of 'e' is never used compile warning
                }
            }
            s.Delete(m2);
            s.Flush();
            s.Close();

            s   = OpenSession();
            mid = s.Save(new Master());
            object mid2 = s.Save(new Master());

            s.Flush();
            s.Disconnect();
            stream = new MemoryStream();
            f.Serialize(stream, s);
            stream.Position = 0;

            s = (ISession)f.Deserialize(stream);
            stream.Close();

            s.Reconnect();
            s.Delete(s.Load(typeof(Master), mid));
            s.Delete(s.Load(typeof(Master), mid2));
            s.Flush();
            s.Close();

            s = OpenSession();
            string db = s.Connection.Database;             //force session to grab a connection

            try
            {
                stream = new MemoryStream();
                f.Serialize(stream, s);
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException, "illegal state");
                s.Close();
                return;
            }
            finally
            {
                stream.Close();
            }
            Assert.IsTrue(false, "serialization should have failed");
        }
Exemplo n.º 8
0
        public void MasterDetail()
        {
            //if( dialect is Dialect.HSQLDialect ) return;

            ISession     s      = OpenSession();
            ITransaction t      = s.BeginTransaction();
            Master       master = new Master();

            Assert.IsNotNull(s.Save(master), "save returned native id");
            object mid = s.GetIdentifier(master);
            Detail d1  = new Detail();

            d1.Master = master;
            object did = s.Save(d1);
            Detail d2  = new Detail();

            d2.I      = 12;
            d2.Master = master;
            Assert.IsNotNull(s.Save(d2), "generated id returned");
            master.AddDetail(d1);
            master.AddDetail(d2);

            if (Dialect.SupportsSubSelects)
            {
                string hql = "from d in class NHibernate.DomainModel.Detail, m in class NHibernate.DomainModel.Master " +
                             "where m = d.Master and m.Outgoing.size = 0 and m.Incoming.size = 0";

                Assert.AreEqual(2, s.CreateQuery(hql).List().Count, "query");
            }
            t.Commit();
            s.Close();

            s      = OpenSession();
            t      = s.BeginTransaction();
            master = (Master)s.Load(typeof(Master), mid);
            IEnumerator enumer = master.Details.GetEnumerator();
            int         i      = 0;

            while (enumer.MoveNext())
            {
                Detail d = (Detail)enumer.Current;
                Assert.AreSame(master, d.Master, "master-detail");
                i++;
            }
            Assert.AreEqual(2, i, "master-detail count");
            t.Commit();
            s.Close();

            s = OpenSession();
            t = s.BeginTransaction();
            Assert.AreEqual(2, s.CreateQuery("select elements(master.Details) from Master master").List().Count);
            t.Commit();
            s.Close();

            s = OpenSession();
            t = s.BeginTransaction();
            IList  list = s.CreateQuery("from Master m left join fetch m.Details").List();
            Master m    = (Master)list[0];

            Assert.IsTrue(NHibernateUtil.IsInitialized(m.Details), "joined fetch should initialize collection");
            Assert.AreEqual(2, m.Details.Count);
            list = s.CreateQuery("from Detail d inner join fetch d.Master").List();
            Detail dt   = (Detail)list[0];
            object dtid = s.GetIdentifier(dt);

            Assert.AreSame(m, dt.Master);
            t.Commit();
            s.Close();

            s    = OpenSession();
            t    = s.BeginTransaction();
            list = s.CreateQuery("select m from Master m1, Master m left join fetch m.Details where m.Name=m1.Name").List();
            Assert.IsTrue(NHibernateUtil.IsInitialized(((Master)list[0]).Details));
            dt = (Detail)s.Load(typeof(Detail), dtid);
            Assert.IsTrue(((Master)list[0]).Details.Contains(dt));
            t.Commit();
            s.Close();

            s    = OpenSession();
            t    = s.BeginTransaction();
            list =
                s.CreateQuery("select m, m1.Name from Master m1, Master m left join fetch m.Details where m.Name=m1.Name").List();
            Master masterFromHql = (Master)((object[])list[0])[0];

            Assert.IsTrue(NHibernateUtil.IsInitialized(masterFromHql.Details));
            dt = (Detail)s.Load(typeof(Detail), dtid);
            Assert.IsTrue(masterFromHql.Details.Contains(dt));

            // This line is commentend in H3.2 tests because it work in the classic parser
            // even if it as no sense ('join fetch' where the 'select' is a scalar)
            // The AST check the case with an Exception
            //list = s.CreateQuery("select m.id from Master m inner join fetch m.Details").List();

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

            s = OpenSession();
            t = s.BeginTransaction();
            Detail dd = (Detail)s.Load(typeof(Detail), did);

            master = dd.Master;
            Assert.IsTrue(master.Details.Contains(dd), "detail-master");
            Assert.AreEqual(2, s.CreateFilter(master.Details, "order by this.I desc").List().Count);
            Assert.AreEqual(2, s.CreateFilter(master.Details, "select this where this.id > -1").List().Count);

            IQuery q = s.CreateFilter(master.Details, "where this.id > :id");

            q.SetInt32("id", -1);
            Assert.AreEqual(2, q.List().Count);

            q = s.CreateFilter(master.Details, "where this.id > :id1 and this.id < :id2");
            q.SetInt32("id1", -1);
            q.SetInt32("id2", 99999999);
            Assert.AreEqual(2, q.List().Count);
            q.SetInt32("id2", -1);
            Assert.AreEqual(0, q.List().Count);

            q    = s.CreateFilter(master.Details, "where this.id in (:ids)");
            list = new ArrayList();
            list.Add(did);
            list.Add((long)-1);
            q.SetParameterList("ids", list);

            Assert.AreEqual(1, q.List().Count);
            Assert.IsTrue(q.Enumerable().GetEnumerator().MoveNext());

            Assert.AreEqual(2, s.CreateFilter(master.Details, "where this.id > -1").List().Count);
            Assert.AreEqual(2, s.CreateFilter(master.Details, "select this.Master where this.id > -1").List().Count);
            Assert.AreEqual(2,
                            s.CreateFilter(master.Details, "select m from m in class Master where this.id > -1 and this.Master=m")
                            .List().Count);
            Assert.AreEqual(0, s.CreateFilter(master.Incoming, "where this.id > -1 and this.Name is not null").List().Count);

            IQuery filter = s.CreateFilter(master.Details, "select max(this.I)");

            enumer = filter.Enumerable().GetEnumerator();
            Assert.IsTrue(enumer.MoveNext());
            Assert.IsTrue(enumer.Current is Int32);

            filter = s.CreateFilter(master.Details, "select max(this.I) group by this.id");
            enumer = filter.Enumerable().GetEnumerator();
            Assert.IsTrue(enumer.MoveNext());
            Assert.IsTrue(enumer.Current is Int32);

            filter = s.CreateFilter(master.Details, "select count(*)");
            enumer = filter.Enumerable().GetEnumerator();
            Assert.IsTrue(enumer.MoveNext());
            Assert.IsTrue(enumer.Current is Int64);

            Assert.AreEqual(2, s.CreateFilter(master.Details, "select this.Master").List().Count);

            IQuery f = s.CreateFilter(master.Details, "select max(this.I) where this.I < :top and this.I>=:bottom");

            f.SetInt32("top", 100);
            f.SetInt32("bottom", 0);

            enumer = f.Enumerable().GetEnumerator();
            Assert.IsTrue(enumer.MoveNext());
            Assert.AreEqual(12, enumer.Current);

            f.SetInt32("top", 2);
            enumer = f.Enumerable().GetEnumerator();
            Assert.IsTrue(enumer.MoveNext());
            Assert.AreEqual(0, enumer.Current);

            f = s.CreateFilter(master.Details, "select max(this.I) where this.I not in (:list)");
            IList coll = new ArrayList();

            coll.Add(-666);
            coll.Add(22);
            coll.Add(0);

            f.SetParameterList("list", coll);
            enumer = f.Enumerable().GetEnumerator();
            Assert.IsTrue(enumer.MoveNext());
            Assert.AreEqual(12, enumer.Current);

            i = 0;
            foreach (Detail d in master.Details)
            {
                Assert.AreSame(master, d.Master, "master-detail");
                s.Delete(d);
                i++;
            }

            Assert.AreEqual(2, i, "master-detail");
            s.Delete(master);
            t.Commit();
            s.Close();
        }
Exemplo n.º 9
0
        public async Task SerializationAsync()
        {
            ISession s   = OpenSession();
            Master   m   = new Master();
            Detail   d1  = new Detail();
            Detail   d2  = new Detail();
            object   mid = await(s.SaveAsync(m));

            d1.Master = (m);
            d2.Master = (m);
            m.AddDetail(d1);
            m.AddDetail(d2);
            if (Dialect is MsSql2000Dialect)
            {
                await(s.SaveAsync(d1));
            }
            else
            {
                await(s.SaveAsync(d1, 666L));
            }
            await(s.FlushAsync());
            s.Disconnect();
            MemoryStream    stream = new MemoryStream();
            BinaryFormatter f      = new BinaryFormatter();

            f.Serialize(stream, s);
            s.Close();
            stream.Position = 0;
            Console.WriteLine(stream.Length);

            s = (ISession)f.Deserialize(stream);
            stream.Close();

            s.Reconnect();
            Master m2 = (Master)await(s.LoadAsync(typeof(Master), mid));

            Assert.IsTrue(m2.Details.Count == 2, "serialized state");
            foreach (Detail d in m2.Details)
            {
                Assert.IsTrue(d.Master == m2, "deserialization");
                try
                {
                    s.GetIdentifier(d);
                    await(s.DeleteAsync(d));
                }
                catch (Exception)
                {
                    // Why are we ignoring exceptions here? /Oskar 2014-08-24
                }
            }
            await(s.DeleteAsync(m2));
            await(s.FlushAsync());
            s.Close();

            s   = OpenSession();
            mid = await(s.SaveAsync(new Master()));
            object mid2 = await(s.SaveAsync(new Master()));

            await(s.FlushAsync());
            s.Disconnect();
            stream = new MemoryStream();
            f.Serialize(stream, s);
            s.Close();
            stream.Position = 0;

            s = (ISession)f.Deserialize(stream);
            stream.Close();

            s.Reconnect();
            await(s.DeleteAsync(await(s.LoadAsync(typeof(Master), mid))));
            await(s.DeleteAsync(await(s.LoadAsync(typeof(Master), mid2))));
            await(s.FlushAsync());
            s.Close();

            s = OpenSession();
            string db = s.Connection.Database;             //force session to grab a connection

            try
            {
                stream = new MemoryStream();
                f.Serialize(stream, s);
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException, "illegal state");
                s.Close();
                return;
            }
            finally
            {
                stream.Close();
            }
            Assert.IsTrue(false, "serialization should have failed");
        }