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 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 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 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 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 EntityJoinForCustomEntityName_Expression()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityCustomEntityName  ejCustomEntity = null;
                    EntityWithNoAssociation root           = null;
                    root = session.QueryOver(() => root)
                           .JoinEntityAlias(() => ejCustomEntity, () => ejCustomEntity.Id == root.CustomEntityNameId, JoinType.InnerJoin, customEntityName)
                           .Take(1)
                           .SingleOrDefault();

                    ejCustomEntity = (EntityCustomEntityName)session.Load(customEntityName, root.CustomEntityNameId);

                    Assert.That(NHibernateUtil.IsInitialized(ejCustomEntity), Is.True);
                    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");
                }
        }
        public void EntityJoinForCompositeKey()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityWithCompositeId   ejComposite = null;
                    EntityWithNoAssociation root        = null;
                    root = session
                           .QueryOver(() => root)
                           .JoinEntityAlias(() => ejComposite, () => root.Composite1Key1 == ejComposite.Key.Id1 && root.Composite1Key2 == ejComposite.Key.Id2)
                           .Take(1).SingleOrDefault();
                    var composite = session.Load <EntityWithCompositeId>(_entityWithCompositeId.Key);

                    Assert.That(NHibernateUtil.IsInitialized(composite), Is.True, "Object must be initialized");
                    Assert.That(composite, Is.EqualTo(_entityWithCompositeId).Using((EntityWithCompositeId x, EntityWithCompositeId y) => (Equals(x.Key, y.Key) && Equals(x.Name, y.Name)) ? 0 : 1));
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
        }
示例#9
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 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");
                }
        }
        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"
                        }
                    };

                    _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);

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

                    session.Save(customEntityName, _entityWithCustomEntityName);

                    _noAssociation = new EntityWithNoAssociation()
                    {
                        Complex1Id         = parent.Id,
                        Complex2Id         = parent.SameTypeChild.Id,
                        Composite1Key1     = _entityWithCompositeId.Key.Id1,
                        Composite1Key2     = _entityWithCompositeId.Key.Id2,
                        Simple1Id          = child1.Id,
                        Simple2Id          = child2.Id,
                        CustomEntityNameId = _entityWithCustomEntityName.Id
                    };

                    session.Save(_noAssociation);

                    session.Flush();
                    transaction.Commit();
                }
        }