-
Notifications
You must be signed in to change notification settings - Fork 933
Improve one-to-one handling in queries #2084
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
4 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
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,279 @@ | ||
using System; | ||
using System.Linq; | ||
using NHibernate.Cfg.MappingSchema; | ||
using NHibernate.Mapping.ByCode; | ||
using NHibernate.Test.Associations.OneToOneFixtureEntities; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.Associations | ||
{ | ||
[TestFixture] | ||
public class OneToOneFixture : TestCaseMappingByCode | ||
{ | ||
[Test] | ||
public void OneToOneCompositeQueryByEntityParam() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var e = session.Query<EntityWithCompositeId>().FirstOrDefault(); | ||
var loadedEntity = session.Query<Parent>().Where(p => p.OneToOneComp == e).FirstOrDefault(); | ||
|
||
Assert.That(loadedEntity, Is.Not.Null); | ||
} | ||
} | ||
|
||
//NH-3778 (GH-1368) | ||
[Test] | ||
public void OneToOneCompositeQueryOverByEntityParam() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var e = session.Query<EntityWithCompositeId>().FirstOrDefault(); | ||
var loadedEntity = session.QueryOver<Parent>().Where(p => p.OneToOneComp == e).SingleOrDefault(); | ||
|
||
Assert.That(loadedEntity, Is.Not.Null); | ||
} | ||
} | ||
|
||
[Test] | ||
public void OneToOneCompositeQueryByKey() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var e = session.Query<EntityWithCompositeId>().FirstOrDefault(); | ||
var loadedEntity = session.Query<Parent>().Where(p => p.OneToOneComp.Key == e.Key).FirstOrDefault(); | ||
|
||
Assert.That(loadedEntity, Is.Not.Null); | ||
} | ||
} | ||
|
||
[Test] | ||
public void OneToOneCompositeQueryOverByKey() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var e = session.Query<EntityWithCompositeId>().FirstOrDefault(); | ||
var loadedEntity = session.QueryOver<Parent>().Where(p => p.OneToOneComp.Key == e.Key).SingleOrDefault(); | ||
|
||
Assert.That(loadedEntity, Is.Not.Null); | ||
} | ||
} | ||
|
||
//NH-3469 (GH-1309) | ||
[Test] | ||
public void OneToOneCompositeQueryByNotNull() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var loadedEntity = session.Query<Parent>().Where(p => p.OneToOneComp != null).FirstOrDefault(); | ||
|
||
Assert.That(loadedEntity, Is.Not.Null); | ||
} | ||
} | ||
|
||
[Test] | ||
public void OneToOneCompositeQueryOverByNotNull() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var loadedEntity = session.QueryOver<Parent>().Where(p => p.OneToOneComp != null).SingleOrDefault(); | ||
|
||
Assert.That(loadedEntity, Is.Not.Null); | ||
} | ||
} | ||
|
||
[Test] | ||
public void OneToOneCompositeQueryCompareWithJoin() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var loadedEntity = session.CreateQuery("select e from Parent p, EntityWithCompositeId e where p.OneToOneComp = e").UniqueResult<EntityWithCompositeId>(); | ||
|
||
Assert.That(loadedEntity, Is.Not.Null); | ||
} | ||
} | ||
|
||
[Explicit("Expression in Restrictions.Where can't recognize direct alias comparison.")] | ||
[Test] | ||
public void OneToOneCompositeQueryOverCompareWithJoin() | ||
{ | ||
using(new SqlLogSpy()) | ||
using (var session = OpenSession()) | ||
{ | ||
Parent parent = null; | ||
EntityWithCompositeId oneToOne = null; | ||
|
||
var loadedEntity = session.QueryOver<Parent>(() => parent) | ||
.JoinEntityAlias(() => oneToOne, () => parent.OneToOneComp == oneToOne) | ||
.SingleOrDefault(); | ||
|
||
Assert.That(loadedEntity, Is.Not.Null); | ||
} | ||
} | ||
|
||
[Test] | ||
public void OneToOneCompositeQueryOverCompareWithJoinById() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var e = session.Query<EntityWithCompositeId>().FirstOrDefault(); | ||
Parent parent = null; | ||
EntityWithCompositeId oneToOne = null; | ||
|
||
var loadedEntity = session.QueryOver<Parent>(() => parent) | ||
.JoinEntityAlias(() => oneToOne, () => parent.OneToOneComp.Key == oneToOne.Key) | ||
.SingleOrDefault(); | ||
|
||
Assert.That(loadedEntity, Is.Not.Null); | ||
} | ||
} | ||
|
||
//GH-2064 | ||
[Test] | ||
public void OneToOneCompositeQuerySelectProjection() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var loadedProjection = session.Query<Parent>().Select(x => new {x.OneToOneComp, x.Key}).FirstOrDefault(); | ||
|
||
Assert.That(loadedProjection.OneToOneComp, Is.Not.Null); | ||
} | ||
} | ||
|
||
//NH-3178 (GH-1125) | ||
[Test] | ||
public void OneToOneQueryOverSelectProjection() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var loadedEntity = session.QueryOver<Parent>() | ||
.Select(x => x.OneToOneComp) | ||
.SingleOrDefault<EntityWithCompositeId>(); | ||
|
||
Assert.That(loadedEntity, Is.Not.Null); | ||
} | ||
} | ||
|
||
#region Test Setup | ||
|
||
protected override HbmMapping GetMappings() | ||
{ | ||
var mapper = new ModelMapper(); | ||
|
||
mapper.Class<EntityWithCompositeId>( | ||
rc => | ||
{ | ||
rc.ComponentAsId( | ||
e => e.Key, | ||
ekm => | ||
{ | ||
ekm.Property(ek => ek.Id1); | ||
ekm.Property(ek => ek.Id2); | ||
}); | ||
|
||
rc.Property(e => e.Name); | ||
}); | ||
|
||
mapper.Class<Parent>( | ||
rc => | ||
{ | ||
rc.ComponentAsId( | ||
e => e.Key, | ||
ekm => | ||
{ | ||
ekm.Property(ek => ek.Id1); | ||
ekm.Property(ek => ek.Id2); | ||
}); | ||
|
||
rc.OneToOne(e => e.OneToOneComp, m => { }); | ||
}); | ||
|
||
return mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (ISession session = OpenSession()) | ||
using (ITransaction transaction = session.BeginTransaction()) | ||
{ | ||
session.Delete("from System.Object"); | ||
|
||
session.Flush(); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var key = new CompositeKey | ||
{ | ||
Id1 = 4, | ||
Id2 = 3, | ||
}; | ||
var oneToOneParent = new Parent() | ||
{ | ||
OneToOneComp = new EntityWithCompositeId | ||
{ | ||
Key = key, | ||
Name = "Composite2" | ||
}, | ||
Key = key | ||
}; | ||
|
||
session.Save(oneToOneParent.OneToOneComp); | ||
session.Save(oneToOneParent); | ||
|
||
session.Flush(); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
#endregion Test Setup | ||
} | ||
|
||
namespace OneToOneFixtureEntities | ||
{ | ||
public class CompositeKey | ||
{ | ||
public int Id1 { get; set; } | ||
public int Id2 { get; set; } | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
var key = obj as CompositeKey; | ||
return key != null | ||
&& Id1 == key.Id1 | ||
&& Id2 == key.Id2; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
var hashCode = -1596524975; | ||
hashCode = hashCode * -1521134295 + Id1.GetHashCode(); | ||
hashCode = hashCode * -1521134295 + Id2.GetHashCode(); | ||
return hashCode; | ||
} | ||
} | ||
|
||
public class EntityWithCompositeId | ||
{ | ||
public virtual CompositeKey Key { get; set; } | ||
public virtual string Name { get; set; } | ||
} | ||
|
||
public class OneToOneEntity | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual string Name { get; set; } | ||
} | ||
|
||
public class Parent | ||
{ | ||
public virtual CompositeKey Key { get; set; } | ||
public virtual EntityWithCompositeId OneToOneComp { 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.
Uh oh!
There was an error while loading. Please reload this page.