public void EntityJoinFoSubquery_JoinQueryOver()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityComplex           ej   = null;
                    EntityWithNoAssociation root = null;

                    EntityComplex           ejSub   = null;
                    EntityWithNoAssociation rootSub = null;

                    var subquery = QueryOver.Of(() => rootSub)
                                   .JoinEntityQueryOver(() => ejSub, () => rootSub.Complex1Id == ejSub.Id)
                                   .Where(x => x.Name == ej.Name)
                                   .Select(x => ejSub.Id);

                    root = session.QueryOver(() => root)
                           .JoinEntityAlias(() => ej, () => root.Complex1Id == ej.Id)
                           .WithSubquery.WhereExists(subquery)
                           .SingleOrDefault();
                    ej = session.Load <EntityComplex>(root.Complex1Id);

                    Assert.That(NHibernateUtil.IsInitialized(ej), Is.True);
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
        }
        public void EntityProjectionForEntityJoin()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntitySimpleChild ejChild1 = null;

                    EntityComplex root = null;
                    EntityComplex st   = null;
                    var           r    = session
                                         .QueryOver(() => root)
                                         .JoinAlias(c => c.SameTypeChild, () => st)
                                         .JoinEntityAlias(() => ejChild1, () => ejChild1.Id == root.Child1.Id)
                                         .Select(
                        Projections.RootEntity(),
                        Projections.Entity(() => st),
                        Projections.Entity(() => ejChild1)
                        )
                                         .SingleOrDefault <object[]>();
                    var rootObj       = (EntityComplex)r[0];
                    var mappedObj     = (EntityComplex)r[1];
                    var entityJoinObj = (EntitySimpleChild)r[2];

                    Assert.That(rootObj, Is.Not.Null);
                    Assert.That(mappedObj, Is.Not.Null);
                    Assert.That(entityJoinObj, Is.Not.Null);
                    Assert.That(NHibernateUtil.IsInitialized(rootObj), Is.True);
                    Assert.That(NHibernateUtil.IsInitialized(mappedObj), Is.True);
                    Assert.That(NHibernateUtil.IsInitialized(entityJoinObj), Is.True);
                }
        }
        public async Task RootEntityProjectionLazyAsync()
        {
            using (var session = OpenSession())
            {
                EntityComplex entityRoot = await(session
                                                 .QueryOver <EntityComplex>()
                                                 .Select(Projections.RootEntity().SetLazy(true))
                                                 .Take(1).SingleOrDefaultAsync());

                Assert.That(NHibernateUtil.IsInitialized(entityRoot), Is.False, "Object must be lazy loaded.");
            }
        }
        protected override void OnSetUp()
        {
            using (var session = OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    var child1 = new EntitySimpleChild
                    {
                        Name = "Child1"
                    };
                    var child2 = new EntitySimpleChild
                    {
                        Name = "Child1"
                    };

                    var parent = new EntityComplex
                    {
                        Name          = "ComplexEnityParent",
                        Child1        = child1,
                        Child2        = child2,
                        LazyProp      = "SomeBigValue",
                        SameTypeChild = new EntityComplex()
                        {
                            Name = "ComplexEntityChild"
                        }
                    };

                    _entityWithCustomEntityName = new EntityCustomEntityName()
                    {
                        Name = "EntityCustomEntityName"
                    };

                    _entityWithCompositeId = new EntityWithCompositeId
                    {
                        Key = new CompositeKey
                        {
                            Id1 = 1,
                            Id2 = 2
                        },
                        Name = "Composite"
                    };

                    session.Save(child1);
                    session.Save(child2);
                    session.Save(parent.SameTypeChild);
                    session.Save(parent);
                    session.Save(_entityWithCompositeId);
                    session.Save(customEntityName, _entityWithCustomEntityName);

                    session.Flush();
                    transaction.Commit();
                }
        }
        public async Task ReadOnlyProjectionAsync()
        {
            using (var session = OpenSession())
            {
                EntityComplex entityRoot = await(session
                                                 .QueryOver <EntityComplex>()
                                                 .Select(Projections.RootEntity())
                                                 .ReadOnly()
                                                 .Take(1).SingleOrDefaultAsync());

                Assert.That(session.IsReadOnly(entityRoot), Is.True, "Object must be loaded readonly.");
            }
        }
        public void CanRowCountWithEntityJoin()
        {
            using (var session = OpenSession())
            {
                EntityComplex           entityComplex = null;
                EntityWithNoAssociation root          = null;
                var query = session.QueryOver(() => root)
                            .JoinEntityAlias(() => entityComplex, Restrictions.Where(() => root.Complex1Id == entityComplex.Id));

                var rowCountQuery = query.ToRowCountQuery();
                int rowCount      = 0;
                Assert.DoesNotThrow(() => rowCount = rowCountQuery.SingleOrDefault <int>(), "row count query should not throw exception");
                Assert.That(rowCount, Is.GreaterThan(0), "row count should be > 0");
            }
        }
        public async Task EntityProjectionWithLazyPropertiesFetchedAsync()
        {
            using (var session = OpenSession())
            {
                EntityComplex entityRoot = await(session
                                                 .QueryOver <EntityComplex>()
                                                 .Where(ec => ec.LazyProp != null)
                                                 .Select(Projections.RootEntity().SetFetchLazyProperties(true))
                                                 .Take(1).SingleOrDefaultAsync());

                Assert.That(entityRoot, Is.Not.Null);
                Assert.That(NHibernateUtil.IsInitialized(entityRoot), Is.True, "Object must be initialized");
                Assert.That(NHibernateUtil.IsPropertyInitialized(entityRoot, nameof(entityRoot.LazyProp)), Is.True, "Lazy property must be initialized");
            }
        }
        public void CanJoinNotAssociatedEntity_Expression()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityComplex           entityComplex = null;
                    EntityWithNoAssociation root          = null;
                    root = session.QueryOver(() => root)
                           .JoinEntityAlias(() => entityComplex, () => root.Complex1Id == entityComplex.Id).Take(1)
                           .SingleOrDefault();
                    entityComplex = session.Load <EntityComplex>(root.Complex1Id);

                    Assert.That(NHibernateUtil.IsInitialized(entityComplex), Is.True);
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
        }
        public async Task CanJoinEntityQueryOverAsync()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityComplex           ejQueryOver = null;
                    EntityWithNoAssociation root        = null;
                    root = await(session.QueryOver(() => root)
                                 .JoinEntityQueryOver(() => ejQueryOver, Restrictions.Where(() => root.Complex1Id == ejQueryOver.Id))
                                 .Take(1)
                                 .SingleOrDefaultAsync());
                    ejQueryOver = await(session.LoadAsync <EntityComplex>(root.Complex1Id));

                    Assert.That(NHibernateUtil.IsInitialized(ejQueryOver), Is.True);
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
        }
        public void SimpleProjectionForEntityJoin()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityComplex           ejComplex = null;
                    EntityWithNoAssociation root      = null;
                    var name = session.QueryOver(() => root)
                               .JoinEntityQueryOver(() => ejComplex, () => root.Complex1Id == ejComplex.Id)
                               .Select((e) => ejComplex.Name)
                               .Take(1)
                               .SingleOrDefault <string>();

                    Assert.That(name, Is.Not.Empty);
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
        }
        public async Task RootEntityProjectionFullyInitializedAndWithUnfetchedLazyPropertiesByDefaultAsync()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    Sfi.Statistics.Clear();
                    EntityComplex entityRoot = await(session
                                                     .QueryOver <EntityComplex>()
                                                     .Where(ec => ec.LazyProp != null)
                                                     .Select(Projections.RootEntity())
                                                     .Take(1).SingleOrDefaultAsync());

                    Assert.That(NHibernateUtil.IsInitialized(entityRoot), Is.True, "Object must be initialized by default");
                    Assert.That(session.IsReadOnly(entityRoot), Is.False, "Object must not be readonly by default");
                    Assert.That(NHibernateUtil.IsPropertyInitialized(entityRoot, nameof(entityRoot.LazyProp)), Is.False, "Lazy properties should not be initialized by default.");
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
        }
        public void NullLeftEntityJoin()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityComplex           ejLeftNull = null;
                    EntityWithNoAssociation root       = null;
                    root = session.QueryOver(() => root)
                           //add some non existent join condition
                           .JoinEntityAlias(() => ejLeftNull, () => ejLeftNull.Id == null, JoinType.LeftOuterJoin)
                           .Take(1)
                           .SingleOrDefault();

                    Assert.That(root, Is.Not.Null, "root should not be null (looks like left join didn't work)");
                    Assert.That(NHibernateUtil.IsInitialized(root), Is.True);
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
        }
Beispiel #13
0
        public async Task NullLeftEntityJoinAsync()
        {
#pragma warning disable CS8073 //The result of the expression is always 'false'
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityComplex           ejLeftNull = null;
                    EntityWithNoAssociation root       = null;
                    root = await(session.QueryOver(() => root)
                                 //add some non existent join condition
                                 .JoinEntityAlias(() => ejLeftNull, () => ejLeftNull.Id == null, JoinType.LeftOuterJoin)
                                 .Take(1)
                                 .SingleOrDefaultAsync());

                    Assert.That(root, Is.Not.Null, "root should not be null (looks like left join didn't work)");
                    Assert.That(NHibernateUtil.IsInitialized(root), Is.True);
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
#pragma warning restore CS8073 //The result of the expression is always 'false'
        }
 public void MixOfJoinsForAssociatedAndNotAssociatedEntities()
 {
     using (var sqlLog = new SqlLogSpy())
         using (var session = OpenSession())
         {
             EntityComplex     root     = null;
             EntityComplex     ejLevel1 = null;
             EntitySimpleChild customChildForEjLevel1             = null;
             EntityComplex     entityComplexForEjLevel1           = null;
             EntitySimpleChild ejLevel2OnEntityComplexForEjLevel1 = null;
             var obj = session
                       .QueryOver(() => root)
                       .JoinEntityAlias(() => ejLevel1, Restrictions.Where(() => ejLevel1.Id == root.SameTypeChild.Id && root.Id != null), JoinType.LeftOuterJoin)
                       .JoinAlias(() => ejLevel1.Child1, () => customChildForEjLevel1, JoinType.InnerJoin)
                       .JoinAlias(() => ejLevel1.SameTypeChild, () => entityComplexForEjLevel1, JoinType.LeftOuterJoin)
                       .JoinEntityAlias(() => ejLevel2OnEntityComplexForEjLevel1, () => entityComplexForEjLevel1.Id == ejLevel2OnEntityComplexForEjLevel1.Id)
                       .Where(() => customChildForEjLevel1.Id != null && ejLevel2OnEntityComplexForEjLevel1.Id != null)
                       .Take(1)
                       .SingleOrDefault <object>();
         }
 }
        public async Task MultipleEntitiesProjectionsToResultTransformerAsync()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    MultipleEntitiesResult r = null;

                    EntitySimpleChild child1          = null;
                    EntitySimpleChild child2          = null;
                    EntityComplex     sameAsRootChild = null;
                    EntitySimpleChild nullListElem    = null;

                    r = await(session
                              .QueryOver <EntityComplex>()
                              .JoinAlias(ep => ep.Child1, () => child1)
                              .JoinAlias(ep => ep.Child2, () => child2)
                              .JoinAlias(ep => ep.SameTypeChild, () => sameAsRootChild)
                              .JoinAlias(ep => ep.ChildrenList, () => nullListElem, JoinType.LeftOuterJoin)
                              .Select(
                                  Projections.RootEntity().WithAlias(nameof(r.Root)),
                                  Projections.Entity(() => child1),
                                  Projections.Property(() => child2.Name).As(nameof(r.Name)),
                                  Projections.Entity(() => child2),
                                  Projections.Property(() => child1.Id),
                                  Projections.Entity(() => sameAsRootChild),
                                  Projections.Entity(() => nullListElem)
                                  )
                              .TransformUsing(Transformers.AliasToBean <MultipleEntitiesResult>())
                              .Take(1)
                              .SingleOrDefaultAsync <MultipleEntitiesResult>());

                    Assert.That(NHibernateUtil.IsInitialized(r.Root), Is.True, "Root must be initialized");
                    Assert.That(NHibernateUtil.IsInitialized(r.Child1), Is.True, "Child1 must be initialized");
                    Assert.That(NHibernateUtil.IsInitialized(r.Child2), Is.True, "Child2 must be initialized");
                    Assert.That(NHibernateUtil.IsInitialized(r.SameAsRootChild), Is.True, "SameAsRootChild must be initialized");
                    Assert.That(r.NullListElem, Is.Null, "NullListElem must be null");
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
        }
        public async Task MultipleEntitiesProjectionsAsync()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityComplex     root            = null;
                    EntitySimpleChild child1          = null;
                    EntitySimpleChild child2          = null;
                    EntityComplex     sameAsRootChild = null;
                    EntitySimpleChild nullListElem    = null;
                    var objects = await(session
                                        .QueryOver <EntityComplex>()
                                        .JoinAlias(ep => ep.Child1, () => child1)
                                        .JoinAlias(ep => ep.Child2, () => child2)
                                        .JoinAlias(ep => ep.SameTypeChild, () => sameAsRootChild)
                                        .JoinAlias(ep => ep.ChildrenList, () => nullListElem, JoinType.LeftOuterJoin)
                                        .Select(
                                            Projections.RootEntity(),
                                            Projections.Entity(() => child1),
                                            Projections.Entity(() => child2),
                                            Projections.Entity(() => sameAsRootChild),
                                            Projections.Entity(() => nullListElem)
                                            )
                                        .Take(1).SingleOrDefaultAsync <object[]>());

                    root            = (EntityComplex)objects[0];
                    child1          = (EntitySimpleChild)objects[1];
                    child2          = (EntitySimpleChild)objects[2];
                    sameAsRootChild = (EntityComplex)objects[3];
                    nullListElem    = (EntitySimpleChild)objects[4];

                    Assert.That(NHibernateUtil.IsInitialized(root), Is.True, "root must be initialized");
                    Assert.That(NHibernateUtil.IsInitialized(child1), Is.True, "child1 must be initialized");
                    Assert.That(NHibernateUtil.IsInitialized(child2), Is.True, "child2 must be initialized");
                    Assert.That(NHibernateUtil.IsInitialized(sameAsRootChild), Is.True, "sameAsRootChild must be initialized");
                    Assert.That(nullListElem, Is.Null, "nullListElem must be null");
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
        }
        public async Task NullLeftEntityJoinWithEntityProjectionAsync()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityComplex           ejLeftNull = null;
                    EntityWithNoAssociation root       = null;
                    var objs = await(session.QueryOver(() => root)
                                     //add some non existent join condition
                                     .JoinEntityAlias(() => ejLeftNull, () => ejLeftNull.Id == null, JoinType.LeftOuterJoin)
                                     .Select((e) => root.AsEntity(), e => ejLeftNull.AsEntity())
                                     .Take(1)
                                     .SingleOrDefaultAsync <object[]>());
                    root       = (EntityWithNoAssociation)objs[0];
                    ejLeftNull = (EntityComplex)objs[1];

                    Assert.That(root, Is.Not.Null, "root should not be null (looks like left join didn't work)");
                    Assert.That(NHibernateUtil.IsInitialized(root), Is.True);
                    Assert.That(ejLeftNull, Is.Null, "Entity join should be null");
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
        }
Beispiel #18
0
        public async Task MixOfJoinsForAssociatedAndNotAssociatedEntitiesAsync()
        {
#pragma warning disable CS8073 //The result of the expression is always 'false'
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityComplex     root     = null;
                    EntityComplex     ejLevel1 = null;
                    EntitySimpleChild customChildForEjLevel1             = null;
                    EntityComplex     entityComplexForEjLevel1           = null;
                    EntitySimpleChild ejLevel2OnEntityComplexForEjLevel1 = null;
                    var obj = await(session
                                    .QueryOver(() => root)
                                    .JoinEntityAlias(() => ejLevel1, Restrictions.Where(() => ejLevel1.Id == root.SameTypeChild.Id && root.Id != null), JoinType.LeftOuterJoin)
                                    .JoinAlias(() => ejLevel1.Child1, () => customChildForEjLevel1, JoinType.InnerJoin)
                                    .JoinAlias(() => ejLevel1.SameTypeChild, () => entityComplexForEjLevel1, JoinType.LeftOuterJoin)
                                    .JoinEntityAlias(() => ejLevel2OnEntityComplexForEjLevel1, () => entityComplexForEjLevel1.Id == ejLevel2OnEntityComplexForEjLevel1.Id)
                                    .Where(() => customChildForEjLevel1.Id != null && ejLevel2OnEntityComplexForEjLevel1.Id != null)
                                    .Take(1)
                                    .SingleOrDefaultAsync <object>());
                }
#pragma warning restore CS8073 //The result of the expression is always 'false'
        }
        public async Task MultipleLazyEntityProjectionsAsync()
        {
            using (var session = OpenSession())
            {
                EntitySimpleChild child1        = null;
                EntityComplex     root          = null;
                EntityComplex     sameTypeChild = null;
                EntitySimpleChild child2        = null;

                var result = await(session
                                   .QueryOver(() => root)
                                   .JoinAlias(ep => ep.SameTypeChild, () => sameTypeChild)
                                   .JoinAlias(ep => ep.Child1, () => child1)
                                   .JoinAlias(ep => ep.Child2, () => child2)
                                   .Select(
                                       Projections.RootEntity().SetLazy(true),
                                       Projections.Entity(() => child1).SetLazy(true),
                                       Projections.Entity(() => sameTypeChild).SetLazy(true),
                                       Projections.Entity(() => child2).SetLazy(true)
                                       )
                                   .Take(1).SingleOrDefaultAsync <object[]>());

                root          = (EntityComplex)result[0];
                child1        = (EntitySimpleChild)result[1];
                sameTypeChild = (EntityComplex)result[2];
                child2        = (EntitySimpleChild)result[3];

                Assert.That(NHibernateUtil.IsInitialized(root), Is.False, "root must be lazy loaded.");
                Assert.That(NHibernateUtil.IsInitialized(sameTypeChild), Is.False, "sameTypeChild must be lazy loaded.");
                Assert.That(NHibernateUtil.IsInitialized(child1), Is.False, "child1 must be lazy loaded.");
                Assert.That(NHibernateUtil.IsInitialized(child2), Is.False, "child2 must be lazy loaded.");

                //make sure objects are populated from different aliases for the same types
                Assert.That(root.Id, Is.Not.EqualTo(sameTypeChild.Id), "Different objects are expected for root and sameTypeChild.");
                Assert.That(child1.Id, Is.Not.EqualTo(child2.Id), "Different objects are expected for child1 and child2.");
            }
        }