Skip to content

Fix property ref handling #1872

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
Oct 24, 2018
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
29 changes: 27 additions & 2 deletions src/NHibernate.Test/Async/NHSpecificTest/NH3480/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ protected override void OnSetUp()
{
using (ITransaction transaction = session.BeginTransaction())
{
var parent1 = new Entity { Id = new Entity.Key() { Id = Guid.NewGuid() }, Name = "Bob", OtherId = 20 };
var parent1 = new Entity
{
Id = new Entity.Key { Id = Guid.NewGuid() },
Name = "Bob",
OtherId = 20,
YetAnotherOtherId = 21
};
parent1.Elements.Add(1);
parent1.Elements.Add(2);
session.Save(parent1);

var child1 = new Child { Name = "Bob1", Parent = parent1 };
Expand Down Expand Up @@ -68,8 +76,25 @@ public async Task TestAsync()
var entity = await (result.SingleAsync());

await (NHibernateUtil.InitializeAsync(entity.Children));
Assert.That(entity.Children, Has.Count.GreaterThan(0));
}
}
}

[Test]
public async Task TestOwnerAsync()
{
using (var session = OpenSession())
using (var t = session.BeginTransaction())
{
var entity = await (session.Query<Entity>().SingleAsync(e => e.Name == "Bob"));

// The Elements collection is mapped with a custom type which assert the owner
// is not null.
await (NHibernateUtil.InitializeAsync(entity.Elements));
Assert.That(entity.Elements, Has.Count.GreaterThan(0));
await (t.CommitAsync());
}
}
}
}
}
125 changes: 125 additions & 0 deletions src/NHibernate.Test/Async/PropertyRef/KeyEntityPropertyRefFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//------------------------------------------------------------------------------
// <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.Linq;
using NUnit.Framework;

namespace NHibernate.Test.PropertyRef
{
using System.Threading.Tasks;
[TestFixture]
public class KeyEntityPropertyRefFixtureAsync : TestCase
{
protected override string[] Mappings => new string[] { "PropertyRef.KeyEntityPropertyRef.hbm.xml" };

protected override string MappingsAssembly => "NHibernate.Test";

private int _aWithItemsId;

protected override void OnSetUp()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var c1 = new C { Name = "Third" };
s.Save(c1);
var c2 = new C { Name = "ThirdBis" };
s.Save(c2);

var b1 = new B { Id = 1, Name = "SecondC1" };
var b2 = new B { Id = 2, Name = "SecondC2" };
s.Save(b1);
s.Save(b2);
var a = new A { Id = 3, Name = "First", C = c1 };
a.CItems.Add(b1);
a.CItems.Add(b2);
_aWithItemsId = (int) s.Save(a);

s.Save(new A { Id = 4, Name = "FirstBis", C = c2 });

t.Commit();
}
}

protected override void OnTearDown()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.CreateQuery("delete from B").ExecuteUpdate();
s.CreateQuery("delete from A").ExecuteUpdate();
s.CreateQuery("delete from C").ExecuteUpdate();
t.Commit();
}
}

[Test]
public async Task PropertyRefLazyLoadAsync()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var a = await (s.GetAsync<A>(_aWithItemsId));

Assert.That(NHibernateUtil.IsInitialized(a.CItems), Is.False, "a with items");
Assert.That(a.CItems, Has.Count.EqualTo(2), "a with items");
await (t.CommitAsync());
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity =
await (s
.Query<A>()
.SingleAsync(a => a.Id != _aWithItemsId));

Assert.That(NHibernateUtil.IsInitialized(entity.CItems), Is.False, "a without items");
Assert.That(entity.CItems, Has.Count.EqualTo(0), "a without items");
await (t.CommitAsync());
}
}

[Test]
public async Task PropertyRefEagerLoadAsync()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity =
await (s
.Query<A>()
.Where(a => a.Id == _aWithItemsId)
.FetchMany(a => a.CItems)
.SingleAsync());

Assert.That(NHibernateUtil.IsInitialized(entity.CItems), Is.True, "a with items");
Assert.That(entity.CItems, Has.Count.EqualTo(2), "a with items");
await (t.CommitAsync());
}

using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var entity =
await (s
.Query<A>()
.Where(a => a.Id != _aWithItemsId)
.FetchMany(a => a.CItems)
.SingleAsync());

Assert.That(NHibernateUtil.IsInitialized(entity.CItems), Is.True, "a without items");
Assert.That(entity.CItems, Has.Count.EqualTo(0), "a without items");
await (t.CommitAsync());
}
}
}
}
148 changes: 148 additions & 0 deletions src/NHibernate.Test/Async/PropertyRef/ManyToManyPropertyRefFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//------------------------------------------------------------------------------
// <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 NHibernate.Criterion;
using NUnit.Framework;

namespace NHibernate.Test.PropertyRef
{
using System.Threading.Tasks;
[TestFixture]
public class ManyToManyPropertyRefFixtureAsync : TestCase
{
protected override string[] Mappings => new[] { "PropertyRef.ManyToManyWithPropertyRef.hbm.xml" };

protected override string MappingsAssembly => "NHibernate.Test";

private object _manyAId;

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var manyA = new ManyA { Number = 6, Value = "a value" };

var manyB1 = new ManyB { Number = 4, Value = "a value of b1" };
var manyB2 = new ManyB { Number = 8, Value = "a value of b2" };
var manyB3 = new ManyB { Number = 12, Value = "a value of b3" };

_manyAId = session.Save(manyA);
session.Save(manyB1);
session.Save(manyB2);
session.Save(manyB3);

manyA.ManyBs.Add(manyB1);
manyA.ManyBs.Add(manyB2);
manyA.ManyBs.Add(manyB3);
transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.Delete("from System.Object");
s.Flush();
t.Commit();
}
}

[Test]
public async Task Getting_a_ManyA_object_with_fetchmode_select_will_workAsync()
{
ManyA loadedManyA;
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
loadedManyA =
await (session
.CreateCriteria<ManyA>()
.Add(Restrictions.IdEq(_manyAId))
// Below Fetch is a no-op indeed, provided the mapping does not ask for eager fetching.
// It is the equivalent of obsoleted SetFetchMode Select, which was also no-op, contrary
// to what the SelectMode xml comment could let think. (This comment was valid only when
// the enum was used for mapping, but not when used ins queries.)
.Fetch(SelectMode.Skip, "ManyBs")
.UniqueResultAsync<ManyA>());
await (NHibernateUtil.InitializeAsync(loadedManyA.ManyBs));
await (transaction.CommitAsync());
}

/******************the select statements *************************************************************
SELECT this_.Id as Id0_0_,
this_.Number as Number0_0_,
this_.Value as Value0_0_
FROM ManyA this_
WHERE this_.Id = 1 /# ?p0 /#

SELECT manybs0_.ManyBNumber as ManyBNum1_1_,
manybs0_.ManyANumber as ManyANum2_1_,
manyb1_.Id as Id2_0_,
manyb1_.Number as Number2_0_,
manyb1_.Value as Value2_0_
FROM ManyBs manybs0_
left outer join ManyB manyb1_ on manybs0_.ManyANumber = manyb1_.Number
WHERE manybs0_.ManyBNumber =6 /# ?p0 #/
*/

Assert.That(loadedManyA.ManyBs.Count, Is.EqualTo(3));
}

[Test, Ignore("Not fixed yet")]
public async Task Getting_a_ManyA_object_with_fetchmode_join_will_workAsync()
{
ManyA loadedManyA;
using (var session = OpenSession())
{
using (var transaction = session.BeginTransaction())
{
loadedManyA =
await (session
.CreateCriteria<ManyA>()
.Add(Restrictions.IdEq(_manyAId))
.Fetch(SelectMode.Fetch, "ManyBs")
.UniqueResultAsync<ManyA>());
await (transaction.CommitAsync());
}
}

/******************the select statments *************************************************************
SELECT this_.Id as Id0_1_,
this_.Number as Number0_1_,
this_.Value as Value0_1_,

manybs2_.ManyBNumber as ManyBNum1_3_,
manyb3_.Id as ManyANum2_3_,
manyb3_.Id as Id2_0_,
manyb3_.Number as Number2_0_,
manyb3_.Value as Value2_0_

FROM ManyA this_

left outer join ManyBs manybs2_ on this_.Number=manybs2_.ManyBNumber
left outer join ManyB manyb3_ on manybs2_.ManyANumber=manyb3_.Number

WHERE this_.Id = 1 /# ?p0 #/
Exception:
System.Collections.Generic.KeyNotFoundException: Der angegebene Schlüssel war nicht im Wörterbuch angegeben.
bei System.ThrowHelper.ThrowKeyNotFoundException()
bei System.Collections.Generic.Dictionary`2.get_Item(TKey key)
bei NHibernate.Persister.Entity.AbstractEntityPersister.GetAppropriateUniqueKeyLoader(String propertyName, IDictionary`2 enabledFilters) in C:\Users\Armin\Projects\NHibernate\branches\2.1.x\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:Zeile 2047.
bei NHibernate.Persister.Entity.AbstractEntityPersister.LoadByUniqueKey(String propertyName, Object uniqueKey, ISessionImplementor session) in C:\Users\Armin\Projects\NHibernate\branches\2.1.x\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:Zeile 2037.
bei NHibernate.Type.EntityType.LoadByUniqueKey(String entityName, String uniqueKeyPropertyName, Object key, ISessionImplementor session) in C:\Users\Armin\Projects\NHibernate\branches\2.1.x\nhibernate\src\NHibernate\Type\EntityType.cs:Zeile 552.
*/

Assert.That(loadedManyA.ManyBs.Count, Is.EqualTo(3));
}
}
}
Loading