public void EntityJoinFoSubquery() { using (var sqlLog = new SqlLogSpy()) using (var session = OpenSession()) { EntityComplex ej = null; EntityWithNoAssociation root = null; var subquery = "from EntityWithNoAssociation rootSub " + "inner join EntityComplex ejSub with rootSub.Complex1Id = ejSub.Id " + "where ejSub.Name = ej.Name"; var objs = session.CreateQuery( "select root, ej from EntityWithNoAssociation root " + "inner join EntityComplex ej with root.Complex1Id = ej.Id " + $"where exists ({subquery})") .UniqueResult <object[]>(); root = (EntityWithNoAssociation)objs[0]; ej = (EntityComplex)objs[1]; Assert.That(NHibernateUtil.IsInitialized(root), Is.True); Assert.That(ej, Is.Not.Null); Assert.That(NHibernateUtil.IsInitialized(ej), Is.True); 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 parent = new EntityComplex { Name = "ComplexEnityParent", LazyProp = "SomeBigValue", SameTypeChild = new EntityComplex() { Name = "ComplexEntityChild" }, SameTypeChild2 = new EntityComplex() { Name = "ComplexEntityChild2" } }; _entityWithCompositeId = new EntityWithCompositeId { Key = new CompositeKey { Id1 = 1, Id2 = 2 }, Name = "Composite" }; session.Save(parent.SameTypeChild); session.Save(parent.SameTypeChild2); 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, CustomEntityNameId = _entityWithCustomEntityName.Id }; session.Save(_noAssociation); session.Flush(); transaction.Commit(); } }
public void NullLeftEntityJoin() { using (var sqlLog = new SqlLogSpy()) using (var session = OpenSession()) { var objs = session.CreateQuery( "select ejLeftNull, root " + "from EntityWithNoAssociation root " + "left join EntityComplex ejLeftNull with ejLeftNull.Id is null") .SetMaxResults(1).UniqueResult <object[]>(); EntityComplex ejLeftNull = (EntityComplex)objs[0]; EntityWithNoAssociation root = (EntityWithNoAssociation)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); Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected"); } }