-
Notifications
You must be signed in to change notification settings - Fork 934
Join-fetching with property-ref results in select n+1 #1492
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
00b36c0
Join-fetching with property-ref results in n+1 select
kfedorchenko 912d210
Mutualize caching by unique key.
fredericDelaporte c628196
Tests adjustment and stale data case.
fredericDelaporte 2d610b1
Merge branch 'master' into 1226
fredericDelaporte 7b74b9d
Add nominal case test.
fredericDelaporte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
164 changes: 164 additions & 0 deletions
164
src/NHibernate.Test/Async/NHSpecificTest/GH1226/Fixture.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
//------------------------------------------------------------------------------ | ||
// <auto-generated> | ||
// This code was generated by AsyncGenerator. | ||
// | ||
// Changes to this file may cause incorrect behavior and will be lost if | ||
// the code is regenerated. | ||
// </auto-generated> | ||
//------------------------------------------------------------------------------ | ||
|
||
|
||
using System.Linq; | ||
using NHibernate.Engine; | ||
using NHibernate.Persister.Entity; | ||
using NUnit.Framework; | ||
using NHibernate.Linq; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH1226 | ||
{ | ||
using System.Threading.Tasks; | ||
[TestFixture] | ||
public class FixtureAsync : BugTestCase | ||
{ | ||
protected override void OnSetUp() | ||
{ | ||
base.OnSetUp(); | ||
|
||
using (var session = OpenSession()) | ||
{ | ||
using (var tx = session.BeginTransaction()) | ||
{ | ||
var bank = new Bank { Code = "01234" }; | ||
session.Save(bank); | ||
|
||
var account = new Account { Bank = bank }; | ||
session.Save(account); | ||
|
||
var account2 = new Account { Bank = bank }; | ||
session.Save(account2); | ||
|
||
tx.Commit(); | ||
} | ||
} | ||
Sfi.Statistics.IsStatisticsEnabled = true; | ||
} | ||
|
||
[Test] | ||
public async Task BankShouldBeJoinFetchedAsync() | ||
{ | ||
// Simple case: nothing already in session. | ||
using (var session = OpenSession()) | ||
using (var tx = session.BeginTransaction()) | ||
{ | ||
var countBeforeQuery = Sfi.Statistics.PrepareStatementCount; | ||
|
||
var accounts = await (session.CreateQuery("from Account a left join fetch a.Bank").ListAsync<Account>()); | ||
var associatedBanks = accounts.Select(x => x.Bank).ToList(); | ||
Assert.That(associatedBanks, Has.All.Matches<object>(NHibernateUtil.IsInitialized), | ||
"One bank or more was lazily loaded."); | ||
|
||
var countAfterQuery = Sfi.Statistics.PrepareStatementCount; | ||
var statementCount = countAfterQuery - countBeforeQuery; | ||
|
||
await (tx.CommitAsync()); | ||
|
||
Assert.That(statementCount, Is.EqualTo(1)); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task InSessionBankShouldBeJoinFetchedAsync() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var tx = session.BeginTransaction()) | ||
{ | ||
// #1226 bug only occurs if the Banks are already in the session cache. | ||
await (session.CreateQuery("from Bank").ListAsync<Bank>()); | ||
|
||
var countBeforeQuery = Sfi.Statistics.PrepareStatementCount; | ||
|
||
var accounts = await (session.CreateQuery("from Account a left join fetch a.Bank").ListAsync<Account>()); | ||
var associatedBanks = accounts.Select(x => x.Bank).ToList(); | ||
Assert.That(associatedBanks, Has.All.Matches<object>(NHibernateUtil.IsInitialized), | ||
"One bank or more was lazily loaded."); | ||
|
||
var countAfterQuery = Sfi.Statistics.PrepareStatementCount; | ||
var statementCount = countAfterQuery - countBeforeQuery; | ||
|
||
await (tx.CommitAsync()); | ||
|
||
Assert.That(statementCount, Is.EqualTo(1)); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task AlteredBankShouldBeJoinFetchedAsync() | ||
{ | ||
using (var s1 = OpenSession()) | ||
{ | ||
using (var tx = s1.BeginTransaction()) | ||
{ | ||
// Put them all in s1 cache. | ||
await (s1.CreateQuery("from Bank").ListAsync()); | ||
await (tx.CommitAsync()); | ||
} | ||
|
||
string oldCode; | ||
const string newCode = "12345"; | ||
// Alter the bank code with another session. | ||
using (var s2 = OpenSession()) | ||
using (var tx2 = s2.BeginTransaction()) | ||
{ | ||
var accounts = await (s2.Query<Account>().ToListAsync()); | ||
foreach (var account in accounts) | ||
account.Bank = null; | ||
await (s2.FlushAsync()); | ||
var bank = await (s2.Query<Bank>().SingleAsync()); | ||
oldCode = bank.Code; | ||
bank.Code = newCode; | ||
await (s2.FlushAsync()); | ||
foreach (var account in accounts) | ||
account.Bank = bank; | ||
await (tx2.CommitAsync()); | ||
} | ||
|
||
// Check querying them with s1 is still consistent | ||
using (var tx = s1.BeginTransaction()) | ||
{ | ||
var accounts = await (s1.CreateQuery("from Account a left join fetch a.Bank").ListAsync<Account>()); | ||
var associatedBanks = accounts.Select(x => x.Bank).ToList(); | ||
Assert.That(associatedBanks, Has.All.Not.Null, | ||
"One bank or more failed loading."); | ||
Assert.That(associatedBanks, Has.All.Matches<object>(NHibernateUtil.IsInitialized), | ||
"One bank or more was lazily loaded."); | ||
Assert.That(associatedBanks, Has.All.Property(nameof(Bank.Code)).EqualTo(oldCode), | ||
"One bank or more has no more the old code."); | ||
|
||
await (tx.CommitAsync()); | ||
// Do not check statements count: we are in a special case defeating the eager fetching, because | ||
// we have stale data in session for the bank code. | ||
// But check that the new code, supposed to be unknown for the session, is not cached. | ||
var persister = Sfi.GetEntityPersister(typeof(Bank).FullName); | ||
var index = ((IUniqueKeyLoadable) persister).GetPropertyIndex(nameof(Bank.Code)); | ||
var type = persister.PropertyTypes[index]; | ||
var euk = new EntityUniqueKey(persister.EntityName, nameof(Bank.Code), newCode, type, Sfi); | ||
Assert.That(s1.GetSessionImplementation().PersistenceContext.GetEntity(euk), | ||
Is.Null, "Found a bank associated to the new code in s1"); | ||
} | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
base.OnTearDown(); | ||
|
||
using (var session = OpenSession()) | ||
using (var tx = session.BeginTransaction()) | ||
{ | ||
session.CreateQuery("delete from Account").ExecuteUpdate(); | ||
session.CreateQuery("delete from Bank").ExecuteUpdate(); | ||
tx.Commit(); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
using System.Linq; | ||
using NHibernate.Engine; | ||
using NHibernate.Persister.Entity; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH1226 | ||
{ | ||
[TestFixture] | ||
public class Fixture : BugTestCase | ||
{ | ||
protected override void OnSetUp() | ||
{ | ||
base.OnSetUp(); | ||
|
||
using (var session = OpenSession()) | ||
{ | ||
using (var tx = session.BeginTransaction()) | ||
{ | ||
var bank = new Bank { Code = "01234" }; | ||
session.Save(bank); | ||
|
||
var account = new Account { Bank = bank }; | ||
session.Save(account); | ||
|
||
var account2 = new Account { Bank = bank }; | ||
session.Save(account2); | ||
|
||
tx.Commit(); | ||
} | ||
} | ||
Sfi.Statistics.IsStatisticsEnabled = true; | ||
} | ||
|
||
[Test] | ||
public void BankShouldBeJoinFetched() | ||
{ | ||
// Simple case: nothing already in session. | ||
using (var session = OpenSession()) | ||
using (var tx = session.BeginTransaction()) | ||
{ | ||
var countBeforeQuery = Sfi.Statistics.PrepareStatementCount; | ||
|
||
var accounts = session.CreateQuery("from Account a left join fetch a.Bank").List<Account>(); | ||
var associatedBanks = accounts.Select(x => x.Bank).ToList(); | ||
Assert.That(associatedBanks, Has.All.Matches<object>(NHibernateUtil.IsInitialized), | ||
"One bank or more was lazily loaded."); | ||
|
||
var countAfterQuery = Sfi.Statistics.PrepareStatementCount; | ||
var statementCount = countAfterQuery - countBeforeQuery; | ||
|
||
tx.Commit(); | ||
|
||
Assert.That(statementCount, Is.EqualTo(1)); | ||
} | ||
} | ||
|
||
[Test] | ||
public void InSessionBankShouldBeJoinFetched() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var tx = session.BeginTransaction()) | ||
{ | ||
// #1226 bug only occurs if the Banks are already in the session cache. | ||
session.CreateQuery("from Bank").List<Bank>(); | ||
|
||
var countBeforeQuery = Sfi.Statistics.PrepareStatementCount; | ||
|
||
var accounts = session.CreateQuery("from Account a left join fetch a.Bank").List<Account>(); | ||
var associatedBanks = accounts.Select(x => x.Bank).ToList(); | ||
Assert.That(associatedBanks, Has.All.Matches<object>(NHibernateUtil.IsInitialized), | ||
"One bank or more was lazily loaded."); | ||
|
||
var countAfterQuery = Sfi.Statistics.PrepareStatementCount; | ||
var statementCount = countAfterQuery - countBeforeQuery; | ||
|
||
tx.Commit(); | ||
|
||
Assert.That(statementCount, Is.EqualTo(1)); | ||
} | ||
} | ||
|
||
[Test] | ||
public void AlteredBankShouldBeJoinFetched() | ||
{ | ||
using (var s1 = OpenSession()) | ||
{ | ||
using (var tx = s1.BeginTransaction()) | ||
{ | ||
// Put them all in s1 cache. | ||
s1.CreateQuery("from Bank").List(); | ||
tx.Commit(); | ||
} | ||
|
||
string oldCode; | ||
const string newCode = "12345"; | ||
// Alter the bank code with another session. | ||
using (var s2 = OpenSession()) | ||
using (var tx2 = s2.BeginTransaction()) | ||
{ | ||
var accounts = s2.Query<Account>().ToList(); | ||
foreach (var account in accounts) | ||
account.Bank = null; | ||
s2.Flush(); | ||
var bank = s2.Query<Bank>().Single(); | ||
oldCode = bank.Code; | ||
bank.Code = newCode; | ||
s2.Flush(); | ||
foreach (var account in accounts) | ||
account.Bank = bank; | ||
tx2.Commit(); | ||
} | ||
|
||
// Check querying them with s1 is still consistent | ||
using (var tx = s1.BeginTransaction()) | ||
{ | ||
var accounts = s1.CreateQuery("from Account a left join fetch a.Bank").List<Account>(); | ||
var associatedBanks = accounts.Select(x => x.Bank).ToList(); | ||
Assert.That(associatedBanks, Has.All.Not.Null, | ||
"One bank or more failed loading."); | ||
Assert.That(associatedBanks, Has.All.Matches<object>(NHibernateUtil.IsInitialized), | ||
"One bank or more was lazily loaded."); | ||
Assert.That(associatedBanks, Has.All.Property(nameof(Bank.Code)).EqualTo(oldCode), | ||
"One bank or more has no more the old code."); | ||
|
||
tx.Commit(); | ||
// Do not check statements count: we are in a special case defeating the eager fetching, because | ||
// we have stale data in session for the bank code. | ||
// But check that the new code, supposed to be unknown for the session, is not cached. | ||
var persister = Sfi.GetEntityPersister(typeof(Bank).FullName); | ||
var index = ((IUniqueKeyLoadable) persister).GetPropertyIndex(nameof(Bank.Code)); | ||
var type = persister.PropertyTypes[index]; | ||
var euk = new EntityUniqueKey(persister.EntityName, nameof(Bank.Code), newCode, type, Sfi); | ||
Assert.That(s1.GetSessionImplementation().PersistenceContext.GetEntity(euk), | ||
Is.Null, "Found a bank associated to the new code in s1"); | ||
} | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
base.OnTearDown(); | ||
|
||
using (var session = OpenSession()) | ||
using (var tx = session.BeginTransaction()) | ||
{ | ||
session.CreateQuery("delete from Account").ExecuteUpdate(); | ||
session.CreateQuery("delete from Bank").ExecuteUpdate(); | ||
tx.Commit(); | ||
} | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/NHibernate.Test/NHSpecificTest/GH1226/Mappings.hbm.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" | ||
assembly="NHibernate.Test" | ||
namespace="NHibernate.Test.NHSpecificTest.GH1226"> | ||
<class name="Account"> | ||
<id name="Id"> | ||
<generator class="guid"/> | ||
</id> | ||
<many-to-one fetch="join" name="Bank" property-ref="Code"/> | ||
</class> | ||
|
||
<class name="Bank"> | ||
<id name="Id"> | ||
<generator class="guid"/> | ||
</id> | ||
<property name="Code" unique="true"/> | ||
</class> | ||
</hibernate-mapping> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH1226 | ||
{ | ||
public class Account | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual Bank Bank { get; set; } | ||
} | ||
|
||
public class Bank | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual string Code { get; set; } | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same