Skip to content

Force join for comparisons in hql when not null entity key can represent null entity #2081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 151 additions & 2 deletions src/NHibernate.Test/Async/Hql/EntityJoinHqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,124 @@ public async Task EntityJoinFoSubqueryAsync()
}
}

[Test]
public async Task EntityJoinWithNullableOneToOneEntityComparisonInWithClausShouldAddJoinAsync()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
var entity =
await (session
.CreateQuery(
"select ex "
+ "from NullableOwner ex "
+ "left join OneToOneEntity st with st = ex.OneToOne "
).SetMaxResults(1)
.UniqueResultAsync<NullableOwner>());

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "OneToOneEntity").Count, Is.EqualTo(2));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}

[Test]
public async Task NullableOneToOneWhereEntityIsNotNullAsync()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
var entity =
await (session
.CreateQuery(
"select ex "
+ "from NullableOwner ex "
+ "where ex.OneToOne is not null "
).SetMaxResults(1)
.UniqueResultAsync<NullableOwner>());

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "OneToOneEntity").Count, Is.EqualTo(1));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}

[Test]
public async Task NullableOneToOneWhereIdIsNotNullAsync()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
var entity =
await (session
.CreateQuery(
"select ex "
+ "from NullableOwner ex "
+ "where ex.OneToOne.Id is not null "
).SetMaxResults(1)
.UniqueResultAsync<NullableOwner>());

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "OneToOneEntity").Count, Is.EqualTo(1));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}

[Test]
public async Task NullablePropRefWhereIdEntityNotNullShouldAddJoinAsync()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
var entity =
await (session
.CreateQuery(
"select ex "
+ "from NullableOwner ex "
+ "where ex.PropRef is not null "
).SetMaxResults(1)
.UniqueResultAsync<NullableOwner>());

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "PropRefEntity").Count, Is.EqualTo(1));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}

[Test]
public async Task NullableOneToOneFetchQueryIsNotAffectedAsync()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
var entity =
await (session
.CreateQuery(
"select ex "
+ "from NullableOwner ex left join fetch ex.OneToOne o "
+ "where o is null "
).SetMaxResults(1)
.UniqueResultAsync<NullableOwner>());

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "OneToOneEntity").Count, Is.EqualTo(1));
}
}

[Test]
public async Task NullableOneToOneFetchQueryIsNotAffected2Async()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
var entity =
await (session
.CreateQuery(
"select ex "
+ "from NullableOwner ex left join fetch ex.OneToOne o "
+ "where o.Id is null "
).SetMaxResults(1)
.UniqueResultAsync<NullableOwner>());

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "OneToOneEntity").Count, Is.EqualTo(1));
}
}

[Test]
public async Task EntityJoinWithEntityComparisonInWithClausShouldNotAddJoinAsync()
{
Expand Down Expand Up @@ -329,7 +447,7 @@ protected override HbmMapping GetMappings()

rc.Property(e => e.Name);
});

mapper.Class<EntityWithCompositeId>(
rc =>
{
Expand Down Expand Up @@ -367,6 +485,38 @@ protected override HbmMapping GetMappings()
rc.Property(e => e.Name);
});

mapper.Class<OneToOneEntity>(
rc =>
{
rc.Id(e => e.Id, m => m.Generator(Generators.GuidComb));
rc.Property(e => e.Name);
});

mapper.Class<PropRefEntity>(
rc =>
{
rc.Id(e => e.Id, m => m.Generator(Generators.GuidComb));
rc.Property(e => e.Name);
rc.Property(e => e.PropertyRef);
});

mapper.Class<NullableOwner>(
rc =>
{
rc.Id(e => e.Id, m => m.Generator(Generators.GuidComb));
rc.Property(e => e.Name);
rc.OneToOne(e => e.OneToOne, m => m.Constrained(false));
rc.ManyToOne(
e => e.PropRef,
m =>
{
m.PropertyRef(nameof(PropRefEntity.PropertyRef));
m.ForeignKey("none");
m.NotFound(NotFoundMode.Ignore);
});
});


return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

Expand Down Expand Up @@ -431,7 +581,6 @@ protected override void OnSetUp()
Composite1Key2 = _entityWithCompositeId.Key.Id2,
CustomEntityNameId = _entityWithCustomEntityName.Id
};

session.Save(_noAssociation);

session.Flush();
Expand Down
154 changes: 152 additions & 2 deletions src/NHibernate.Test/Hql/EntityJoinHqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,124 @@ public void EntityJoinFoSubquery()
}
}

[Test]
public void EntityJoinWithNullableOneToOneEntityComparisonInWithClausShouldAddJoin()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
var entity =
session
.CreateQuery(
"select ex "
+ "from NullableOwner ex "
+ "left join OneToOneEntity st with st = ex.OneToOne "
).SetMaxResults(1)
.UniqueResult<NullableOwner>();

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "OneToOneEntity").Count, Is.EqualTo(2));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}

[Test]
public void NullableOneToOneWhereEntityIsNotNull()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
var entity =
session
.CreateQuery(
"select ex "
+ "from NullableOwner ex "
+ "where ex.OneToOne is not null "
).SetMaxResults(1)
.UniqueResult<NullableOwner>();

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "OneToOneEntity").Count, Is.EqualTo(1));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}

[Test]
public void NullableOneToOneWhereIdIsNotNull()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
var entity =
session
.CreateQuery(
"select ex "
+ "from NullableOwner ex "
+ "where ex.OneToOne.Id is not null "
).SetMaxResults(1)
.UniqueResult<NullableOwner>();

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "OneToOneEntity").Count, Is.EqualTo(1));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}

[Test]
public void NullablePropRefWhereIdEntityNotNullShouldAddJoin()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
var entity =
session
.CreateQuery(
"select ex "
+ "from NullableOwner ex "
+ "where ex.PropRef is not null "
).SetMaxResults(1)
.UniqueResult<NullableOwner>();

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "PropRefEntity").Count, Is.EqualTo(1));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}

[Test]
public void NullableOneToOneFetchQueryIsNotAffected()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
var entity =
session
.CreateQuery(
"select ex "
+ "from NullableOwner ex left join fetch ex.OneToOne o "
+ "where o is null "
).SetMaxResults(1)
.UniqueResult<NullableOwner>();

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "OneToOneEntity").Count, Is.EqualTo(1));
}
}

[Test]
public void NullableOneToOneFetchQueryIsNotAffected2()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
var entity =
session
.CreateQuery(
"select ex "
+ "from NullableOwner ex left join fetch ex.OneToOne o "
+ "where o.Id is null "
).SetMaxResults(1)
.UniqueResult<NullableOwner>();

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "OneToOneEntity").Count, Is.EqualTo(1));
}
}

[Test]
public void EntityJoinWithEntityComparisonInWithClausShouldNotAddJoin()
{
Expand Down Expand Up @@ -334,7 +452,7 @@ protected override HbmMapping GetMappings()

rc.Property(e => e.Name);
});

mapper.Class<EntityWithCompositeId>(
rc =>
{
Expand Down Expand Up @@ -372,6 +490,39 @@ protected override HbmMapping GetMappings()
rc.Property(e => e.Name);
});

mapper.Class<OneToOneEntity>(
rc =>
{
rc.Id(e => e.Id, m => m.Generator(Generators.GuidComb));
rc.Property(e => e.Name);
});

mapper.Class<PropRefEntity>(
rc =>
{
rc.Id(e => e.Id, m => m.Generator(Generators.GuidComb));
rc.Property(e => e.Name);
rc.Property(e => e.PropertyRef, m => m.Column("EntityPropertyRef"));
});

mapper.Class<NullableOwner>(
rc =>
{
rc.Id(e => e.Id, m => m.Generator(Generators.GuidComb));
rc.Property(e => e.Name);
rc.OneToOne(e => e.OneToOne, m => m.Constrained(false));
rc.ManyToOne(
e => e.PropRef,
m =>
{
m.Column("OwnerPropertyRef");
m.PropertyRef(nameof(PropRefEntity.PropertyRef));
m.ForeignKey("none");
m.NotFound(NotFoundMode.Ignore);
});
});


return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

Expand Down Expand Up @@ -436,7 +587,6 @@ protected override void OnSetUp()
Composite1Key2 = _entityWithCompositeId.Key.Id2,
CustomEntityNameId = _entityWithCustomEntityName.Id
};

session.Save(_noAssociation);

session.Flush();
Expand Down
Loading