Beispiel #1
0
        public void TestLazyLoadNoAdd()
        {
            int parentId = 0;

            using (ISession s1 = OpenSession())
                using (ITransaction t1 = s1.BeginTransaction())
                {
                    // create a new
                    LLParent     parent = new LLParent();
                    LLChildNoAdd child  = new LLChildNoAdd();
                    parent.ChildrenNoAdd.Add(child);
                    child.Parent = parent;

                    s1.Save(parent);
                    parentId = (int)s1.GetIdentifier(parent);

                    t1.Commit();
                }

            // try to Load the object to make sure the save worked
            using (ISession s2 = OpenSession())
                using (ITransaction t2 = s2.BeginTransaction())
                {
                    LLParent parent2 = (LLParent)s2.Load(typeof(LLParent), parentId);
                    Assert.AreEqual(1, parent2.ChildrenNoAdd.Count);
                }

            using (ISession session = sessions.OpenSession())
            {
                session.Delete("from LLParent");
                session.Flush();
            }
        }
Beispiel #2
0
        public void TestLoadChildFirst()
        {
            int parentId = 0;
            int childId  = 0;

            using (ISession s1 = OpenSession())
                using (ITransaction t1 = s1.BeginTransaction())
                {
                    // create a new
                    LLParent     parent = new LLParent();
                    LLChildNoAdd child  = new LLChildNoAdd();
                    parent.ChildrenNoAdd.Add(child);
                    child.Parent = parent;

                    s1.Save(parent);
                    parentId = (int)s1.GetIdentifier(parent);
                    childId  = (int)s1.GetIdentifier(child);

                    t1.Commit();
                }

            // try to Load the object to make sure the save worked
            using (ISession s2 = OpenSession())
                using (ITransaction t2 = s2.BeginTransaction())
                {
                    LLChildNoAdd child2 = (LLChildNoAdd)s2.Load(typeof(LLChildNoAdd), childId);
                    Assert.AreEqual(parentId, (int)s2.GetIdentifier(child2.Parent));
                }
        }
Beispiel #3
0
        public async Task TestLoadParentFirstAsync()
        {
            int parentId = 0;

            using (ISession s1 = OpenSession())
                using (ITransaction t1 = s1.BeginTransaction())
                {
                    // create a new
                    LLParent     parent = new LLParent();
                    LLChildNoAdd child  = new LLChildNoAdd();
                    parent.ChildrenNoAdd.Add(child);
                    child.Parent = parent;

                    await(s1.SaveAsync(parent));
                    parentId = (int)s1.GetIdentifier(parent);

                    await(t1.CommitAsync());
                }

            // try to Load the object to make sure the save worked
            using (ISession s2 = OpenSession())
                using (ITransaction t2 = s2.BeginTransaction())
                {
                    LLParent parent2 = (LLParent)await(s2.LoadAsync(typeof(LLParent), parentId));
                    Assert.AreEqual(1, parent2.ChildrenNoAdd.Count);
                }
        }
Beispiel #4
0
        public void TestLazyLoad()
        {
            int parentId = 0;

            using (ISession s1 = OpenSession())
                using (ITransaction t1 = s1.BeginTransaction())
                {
                    // create a new
                    LLParent parent = new LLParent();
                    LLChild  child  = new LLChild();
                    child.Parent = parent;

                    s1.Save(parent);
                    parentId = (int)s1.GetIdentifier(parent);

                    t1.Commit();
                }

            try
            {
                // try to Load the object to get the exception
                using (ISession s2 = OpenSession())
                    using (ITransaction t2 = s2.BeginTransaction())
                    {
                        LLParent parent2 = (LLParent)s2.Load(typeof(LLParent), parentId);

                        // this should throw the exception - the property setter access is not mapped correctly.
                        // Because it maintains logic to maintain the collection during the property set it should
                        // tell NHibernate to skip the setter and access the field.  If it doesn't, then throw
                        // a LazyInitializationException.
                        Assert.Throws <LazyInitializationException>(() => { int count = parent2.Children.Count; });
                    }
            }
            finally
            {
                // Need to delete the objects using direct SQL in this case,
                // because of loading problems.
                ExecuteStatement("delete from LLChild");
                ExecuteStatement("delete from LLParent");
            }
        }