-
Notifications
You must be signed in to change notification settings - Fork 934
Fix querying with delayed entity insert #2416
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
971b0b0
#2414 fix error when query with delayed insert entity in where expres…
galamai e297030
Merge branch 'master' into master
galamai 2064694
#2414 Add new tests
galamai cb43e11
Merge branch 'master' of https://github.com/galamai/nhibernate-core
galamai 4cdc6d9
Merge branch 'master' into master
hazzik 4a68d56
Generate async files
github-actions[bot] 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
121 changes: 121 additions & 0 deletions
121
src/NHibernate.Test/Async/NHSpecificTest/GH2414/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,121 @@ | ||
//------------------------------------------------------------------------------ | ||
// <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; | ||
using System.Linq; | ||
using NHibernate.Cfg.MappingSchema; | ||
using NHibernate.Mapping.ByCode; | ||
using NUnit.Framework; | ||
using NHibernate.Linq; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH2414 | ||
{ | ||
using System.Threading.Tasks; | ||
/// <summary> | ||
/// Fixture using 'by code' mappings | ||
/// </summary> | ||
/// <remarks> | ||
/// This fixture is identical to <see cref="FixtureAsync" /> except the <see cref="Entity" /> mapping is performed | ||
/// by code in the GetMappings method, and does not require the <c>Mappings.hbm.xml</c> file. Use this approach | ||
/// if you prefer. | ||
/// </remarks> | ||
[TestFixture] | ||
public class FixtureAsync : TestCaseMappingByCode | ||
{ | ||
protected override HbmMapping GetMappings() | ||
{ | ||
var mapper = new ModelMapper(); | ||
mapper.Class<Entity>(rc => | ||
{ | ||
rc.Id(x => x.Id, m => m.Generator(Generators.Identity)); | ||
rc.Property(x => x.Name); | ||
}); | ||
mapper.Class<EntityWithGuidKey>(rc => | ||
{ | ||
rc.Id(x => x.Id, m => m.Generator(Generators.Guid)); | ||
rc.Property(x => x.Name); | ||
}); | ||
|
||
return mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||
} | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var e1 = new Entity { Name = "Bob" }; | ||
session.Save(e1); | ||
|
||
var e2 = new Entity { Name = "Sally" }; | ||
session.Save(e2); | ||
|
||
var e3 = new EntityWithGuidKey() { Id = Guid.NewGuid(), Name = "Bob" }; | ||
session.Save(e3); | ||
|
||
var e4 = new EntityWithGuidKey() { Id = Guid.NewGuid(), Name = "Sally" }; | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
// The HQL delete does all the job inside the database without loading the entities, but it does | ||
// not handle delete order for avoiding violating constraints if any. Use | ||
// session.Delete("from System.Object"); | ||
// instead if in need of having NHbernate ordering the deletes, but this will cause | ||
// loading the entities in the session. | ||
session.CreateQuery("delete from System.Object").ExecuteUpdate(); | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task QueryWithEqualDelayedPostInsertIdentifierInWhereAsync() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var entity = new Entity() { Name = "NewEntity" }; | ||
await (session.PersistAsync(entity)); | ||
var result = await (session.Query<Entity>().Where(x => x == entity).ToListAsync()); | ||
Assert.That(result, Has.Count.EqualTo(0)); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task QueryWithEqualDelayedPostInsertIdentifierInWhereWithGuidKeyEntityAsync() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var entity = new EntityWithGuidKey() { Name = "NewEntity" }; | ||
await (session.PersistAsync(entity)); | ||
var result = await (session.Query<EntityWithGuidKey>().Where(x => x == entity).ToListAsync()); | ||
Assert.That(result, Has.Count.EqualTo(0)); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task QueryWithNotEqualDelayedPostInsertIdentifierInWhereAsync() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var entity = new Entity() { Name = "NewEntity" }; | ||
await (session.PersistAsync(entity)); | ||
var result = await (session.Query<Entity>().Where(x => x != entity).ToListAsync()); | ||
Assert.That(result, Has.Count.EqualTo(2)); | ||
} | ||
} | ||
} | ||
} |
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,10 @@ | ||
using System; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH2414 | ||
{ | ||
class Entity | ||
{ | ||
public virtual int Id { get; set; } | ||
public virtual string Name { get; set; } | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/NHibernate.Test/NHSpecificTest/GH2414/EntityWithGuidKey.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,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH2414 | ||
{ | ||
class EntityWithGuidKey | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual string Name { get; set; } | ||
} | ||
} |
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,109 @@ | ||
using System; | ||
using System.Linq; | ||
using NHibernate.Cfg.MappingSchema; | ||
using NHibernate.Mapping.ByCode; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH2414 | ||
{ | ||
/// <summary> | ||
/// Fixture using 'by code' mappings | ||
/// </summary> | ||
/// <remarks> | ||
/// This fixture is identical to <see cref="Fixture" /> except the <see cref="Entity" /> mapping is performed | ||
/// by code in the GetMappings method, and does not require the <c>Mappings.hbm.xml</c> file. Use this approach | ||
/// if you prefer. | ||
/// </remarks> | ||
[TestFixture] | ||
public class Fixture : TestCaseMappingByCode | ||
{ | ||
protected override HbmMapping GetMappings() | ||
{ | ||
var mapper = new ModelMapper(); | ||
mapper.Class<Entity>(rc => | ||
{ | ||
rc.Id(x => x.Id, m => m.Generator(Generators.Identity)); | ||
rc.Property(x => x.Name); | ||
}); | ||
mapper.Class<EntityWithGuidKey>(rc => | ||
{ | ||
rc.Id(x => x.Id, m => m.Generator(Generators.Guid)); | ||
rc.Property(x => x.Name); | ||
}); | ||
|
||
return mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||
} | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var e1 = new Entity { Name = "Bob" }; | ||
session.Save(e1); | ||
|
||
var e2 = new Entity { Name = "Sally" }; | ||
session.Save(e2); | ||
|
||
var e3 = new EntityWithGuidKey() { Id = Guid.NewGuid(), Name = "Bob" }; | ||
session.Save(e3); | ||
|
||
var e4 = new EntityWithGuidKey() { Id = Guid.NewGuid(), Name = "Sally" }; | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
// The HQL delete does all the job inside the database without loading the entities, but it does | ||
// not handle delete order for avoiding violating constraints if any. Use | ||
// session.Delete("from System.Object"); | ||
// instead if in need of having NHbernate ordering the deletes, but this will cause | ||
// loading the entities in the session. | ||
session.CreateQuery("delete from System.Object").ExecuteUpdate(); | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
|
||
[Test] | ||
public void QueryWithEqualDelayedPostInsertIdentifierInWhere() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var entity = new Entity() { Name = "NewEntity" }; | ||
session.Persist(entity); | ||
var result = session.Query<Entity>().Where(x => x == entity).ToList(); | ||
Assert.That(result, Has.Count.EqualTo(0)); | ||
} | ||
} | ||
|
||
[Test] | ||
public void QueryWithEqualDelayedPostInsertIdentifierInWhereWithGuidKeyEntity() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var entity = new EntityWithGuidKey() { Name = "NewEntity" }; | ||
session.Persist(entity); | ||
var result = session.Query<EntityWithGuidKey>().Where(x => x == entity).ToList(); | ||
Assert.That(result, Has.Count.EqualTo(0)); | ||
} | ||
} | ||
|
||
[Test] | ||
public void QueryWithNotEqualDelayedPostInsertIdentifierInWhere() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var entity = new Entity() { Name = "NewEntity" }; | ||
session.Persist(entity); | ||
var result = session.Query<Entity>().Where(x => x != entity).ToList(); | ||
Assert.That(result, Has.Count.EqualTo(2)); | ||
} | ||
} | ||
} | ||
} |
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
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
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.
This assumes the identifier is compatible with an
int
, and this assumes0
is not a valid value for it. This will be wrong in many cases.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.
DelayedPostInsertIdentifier exists only for auto-increment fields, so I believe that the value 0 in this case is correct
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.
I compared this behavior with entity framework core 3 comparison by reference for an entity that is not yet stored in the table returns exactly parameter 0 in all cases
Uh oh!
There was an error while loading. Please reload this page.
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.
What if
entity.Id
is not 0 for transient entity and for instance it's -1?Not true for the following SQL Server table: