-
Notifications
You must be signed in to change notification settings - Fork 936
Fix key type mismatch in EntityUniqueKey #1647
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
fredericDelaporte
merged 3 commits into
nhibernate:master
from
fredericDelaporte:GH1645
Apr 10, 2018
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
66 changes: 66 additions & 0 deletions
66
src/NHibernate.Test/Async/NHSpecificTest/GH1645/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,66 @@ | ||
//------------------------------------------------------------------------------ | ||
// <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 NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH1645 | ||
{ | ||
using System.Threading.Tasks; | ||
[TestFixture] | ||
public class FixtureAsync : BugTestCase | ||
{ | ||
private Guid _superParentId; | ||
private Guid _parentId; | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var p = new Parent(); | ||
session.Save(p); | ||
_parentId = p.Id; | ||
|
||
_superParentId = (Guid) session.Save(new SuperParent { Parent = p }); | ||
|
||
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 SOEOnLoadAsync() | ||
{ | ||
using (var session = OpenSession()) | ||
using (session.BeginTransaction()) | ||
{ | ||
var superParent = await (session.LoadAsync<SuperParent>(_superParentId)); | ||
Assert.That(() => NHibernateUtil.InitializeAsync(superParent), Throws.Nothing); | ||
Assert.That(() => NHibernateUtil.InitializeAsync(superParent.Parent), Throws.Nothing); | ||
} | ||
} | ||
} | ||
} |
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,67 @@ | ||
using System; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH1645 | ||
{ | ||
public abstract class EntityBase | ||
{ | ||
public virtual Guid Id { get; protected set; } | ||
|
||
public static bool operator ==(EntityBase left, EntityBase right) | ||
{ | ||
return Equals(left, right); | ||
} | ||
|
||
public static bool operator !=(EntityBase left, EntityBase right) | ||
{ | ||
return !Equals(left, right); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
return Equals(obj as EntityBase); | ||
} | ||
|
||
public virtual bool Equals(EntityBase other) | ||
{ | ||
if (other == null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (ReferenceEquals(this, other)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (!IsTransient(this) && !IsTransient(other) && Equals(Id, other.Id)) | ||
{ | ||
var otherType = other.GetUnproxiedType(); | ||
var thisType = GetUnproxiedType(); | ||
return thisType.IsAssignableFrom(otherType) || | ||
otherType.IsAssignableFrom(thisType); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
if (Equals(Id, default(Guid))) | ||
{ | ||
return base.GetHashCode(); | ||
} | ||
|
||
return Id.GetHashCode(); | ||
} | ||
|
||
private static bool IsTransient(EntityBase obj) | ||
{ | ||
return obj != null && Equals(obj.Id, default(Guid)); | ||
} | ||
|
||
private System.Type GetUnproxiedType() | ||
{ | ||
return GetType(); | ||
} | ||
} | ||
} |
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,55 @@ | ||
using System; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH1645 | ||
{ | ||
[TestFixture] | ||
public class Fixture : BugTestCase | ||
{ | ||
private Guid _superParentId; | ||
private Guid _parentId; | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var p = new Parent(); | ||
session.Save(p); | ||
_parentId = p.Id; | ||
|
||
_superParentId = (Guid) session.Save(new SuperParent { Parent = p }); | ||
|
||
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 SOEOnLoad() | ||
{ | ||
using (var session = OpenSession()) | ||
using (session.BeginTransaction()) | ||
{ | ||
var superParent = session.Load<SuperParent>(_superParentId); | ||
Assert.That(() => NHibernateUtil.Initialize(superParent), Throws.Nothing); | ||
Assert.That(() => NHibernateUtil.Initialize(superParent.Parent), Throws.Nothing); | ||
} | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/NHibernate.Test/NHSpecificTest/GH1645/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,15 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" | ||
namespace="NHibernate.Test.NHSpecificTest.GH1645"> | ||
|
||
<class name="SuperParent"> | ||
<id name="Id" generator="guid.comb"/> | ||
<many-to-one name="Parent"/> | ||
</class> | ||
|
||
<class name="Parent"> | ||
<id name="Id" generator="guid.comb"/> | ||
<one-to-one name="SuperParent" property-ref="Parent"/> | ||
</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,7 @@ | ||
namespace NHibernate.Test.NHSpecificTest.GH1645 | ||
{ | ||
public class Parent : EntityBase | ||
{ | ||
public virtual SuperParent SuperParent { 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,7 @@ | ||
namespace NHibernate.Test.NHSpecificTest.GH1645 | ||
{ | ||
public class SuperParent : EntityBase | ||
{ | ||
public virtual Parent Parent { 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
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.
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.
EntityUniqueKey
was assuming the value to be always semi-resolved (to be the identifier instead of the entity for an entity type). It was always computing the value hashcode with the semi-resolved type, which is the identifier type for an entity.But this has changed with #1492, which caches unique keys also for already fully loaded entities. The value hashcode must then be computed with the entity type, which accounts for proxies.
Otherwise the proxy loading may get triggered while the code is already loading the proxy, which results in a stack overflow.
The type to use is now fully computed before constructing
EntityUniqueKey
.It is constructed in only two places: in the loader, see its changes; in the
EntityType.LoadByUniqueKey
, which usesGetIdentifierOrUniqueKeyType
which yields types having themselves as semi-resolved types. (For being extra-safe, a call toGetSemiResolvedType
could still be added inLoadByUniqueKey
just in case.)Ideally
semiResolvedKey
parameter should be renamed simplykey
(no more assumptions). But renaming a public method parameter is a gray area possible breaking change (if anyone has written a call explicitly naming parameters), and I avoid doing this one in patch releases.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.
Let's do this.
Please add a TODO for 6.0