public void FetchWithAliasedJoinFuture()
        {
            using (var session = OpenSession())
            {
                EntityComplex     alias  = null;
                EntitySimpleChild child1 = null;
                var list = session.QueryOver <EntityComplex>(() => alias)
                           .Where(ec => ec.Id == _parentEntityComplexId)
                           .JoinQueryOver(() => alias.Child1, () => child1)
                           .Fetch(SelectMode.Fetch, () => alias.ChildrenList)
                           .TransformUsing(Transformers.DistinctRootEntity)
                           .Future()
                           .GetEnumerable()
                           .ToList();

                var childList = list[0].ChildrenList;
                Assert.That(list[0].ChildrenList.Count, Is.GreaterThan(1));
                Assert.That(list[0].ChildrenList, Is.EqualTo(list[0].ChildrenList.OrderByDescending(c => c.OrderIdx)), "wrong order");
            }
        }
        public void CacheableFetchWithAliasedJoinFuture()
        {
            using (var session = OpenSession())
            {
                EntityComplex     alias  = null;
                EntitySimpleChild child1 = null;
                var list = session.QueryOver <EntityComplex>(() => alias)
                           .Where(ec => ec.Id == _parentEntityComplexId)
                           .JoinQueryOver(() => alias.Child1, () => child1)
                           .Fetch(SelectMode.Fetch, () => alias.ChildrenList)
                           .TransformUsing(Transformers.DistinctRootEntity)
                           .Cacheable()
                           .Future()
                           .GetEnumerable()
                           .ToList();
                EntityComplex value = null;
                Assert.DoesNotThrow(() => value = list[0]);
                Assert.That(value, Is.Not.Null);
            }

            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityComplex     alias  = null;
                    EntitySimpleChild child1 = null;
                    var list = session.QueryOver <EntityComplex>(() => alias)
                               .Where(ec => ec.Id == _parentEntityComplexId)
                               .JoinQueryOver(() => alias.Child1, () => child1)
                               .Fetch(SelectMode.Fetch, () => alias.ChildrenList)
                               .TransformUsing(Transformers.DistinctRootEntity)
                               .Cacheable()
                               .Future()
                               .ToList();
                    EntityComplex value = null;
                    Assert.DoesNotThrow(() => value = list[0]);
                    Assert.That(value, Is.Not.Null);

                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(0), "Query is expected to be retrieved from cache");
                }
        }
Example #3
0
        public async Task SelectModeJoinOnlyEntityJoinAsync()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityComplex     parentJoin = null;
                    EntitySimpleChild rootChild  = null;
                    rootChild = await(session.QueryOver(() => rootChild)
                                      .JoinEntityQueryOver(() => parentJoin, Restrictions.Where(() => rootChild.ParentId == parentJoin.Id))
                                      .Fetch(SelectMode.JoinOnly, a => a)
                                      .Take(1)
                                      .SingleOrDefaultAsync());

                    parentJoin = await(session.LoadAsync <EntityComplex>(rootChild.ParentId));

                    Assert.That(rootChild, Is.Not.Null);
                    Assert.That(NHibernateUtil.IsInitialized(rootChild), Is.True);
                    Assert.That(rootChild.ParentId, Is.Not.Null);
                    Assert.That(NHibernateUtil.IsInitialized(parentJoin), Is.False, "Entity Join must not be initialized.");
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
        }
Example #4
0
        public async Task SelectModeFetchLazyPropertiesForEntityJoinAsync()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityComplex     parentJoin = null;
                    EntitySimpleChild rootChild  = null;
                    rootChild = await(session.QueryOver(() => rootChild)
                                      .JoinEntityQueryOver(() => parentJoin, Restrictions.Where(() => rootChild.ParentId == parentJoin.Id))
                                      .Fetch(SelectMode.FetchLazyProperties, ec => ec)
                                      .Take(1)
                                      .SingleOrDefaultAsync());
                    parentJoin = await(session.LoadAsync <EntityComplex>(rootChild.ParentId));

                    Assert.That(rootChild, Is.Not.Null);

                    Assert.That(NHibernateUtil.IsInitialized(rootChild), Is.True);
                    Assert.That(NHibernateUtil.IsInitialized(parentJoin), Is.True);
                    Assert.That(NHibernateUtil.IsPropertyInitialized(parentJoin, nameof(parentJoin.LazyProp)), Is.Not.Null.Or.Empty);
                    Assert.That(parentJoin.LazyProp, Is.Not.Null.Or.Empty);

                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
        }
Example #5
0
        protected override void OnSetUp()
        {
            using (var session = OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    var child1 = new EntitySimpleChild
                    {
                        Name     = "Child1",
                        LazyProp = "LazyFromSimpleChild1",
                        Children = new List <Level2Child>
                        {
                            new Level2Child()
                            {
                                Name     = "Level2.1",
                                Children = new List <Level3Child>
                                {
                                    new Level3Child
                                    {
                                        Name = "Level3.1.1",
                                    },
                                    new Level3Child
                                    {
                                        Name = "Level3.1.2"
                                    },
                                }
                            },
                            new Level2Child
                            {
                                Name     = "Level2.2",
                                Children = new List <Level3Child>
                                {
                                    new Level3Child
                                    {
                                        Name = "Level3.2.1"
                                    },
                                    new Level3Child
                                    {
                                        Name = "Level3.2.2"
                                    },
                                }
                            }
                        },
                        OrderIdx = 100
                    };

                    var child2 = new EntitySimpleChild
                    {
                        Name     = "Child2",
                        LazyProp = "LazyFromSimpleChild2",
                    };

                    var child3 = new EntitySimpleChild
                    {
                        Name     = "Child3",
                        OrderIdx = 0
                    };
                    var child4 = new EntitySimpleChild
                    {
                        Name     = "Child4",
                        OrderIdx = 50
                    };

                    var parent = new EntityComplex
                    {
                        Name          = "ComplexEntityParent",
                        Child1        = child1,
                        Child2        = child2,
                        LazyProp      = "SomeBigValue",
                        SameTypeChild = new EntityComplex()
                        {
                            Name = "ComplexEntityChild"
                        },
                        ChildrenList = new List <EntitySimpleChild> {
                            child3, child1, child4
                        },
                        ChildrenListEmpty = new List <EntityComplex> {
                        },
                    };
                    session.Save(new EntityEager()
                    {
                        Name         = "Eager",
                        ChildrenList = new List <EntityEagerChild>
                        {
                            new EntityEagerChild()
                            {
                                Name = "EagerChild"
                            }
                        }
                    });

                    session.Save(child1);
                    session.Save(child2);
                    session.Save(parent.SameTypeChild);
                    session.Save(parent);

                    session.Flush();
                    transaction.Commit();
                    _parentEntityComplexId = parent.Id;
                }
        }