Skip to content

Fix SelectMode.ChildFetch for many-to-many #3140

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 3 commits into from
Aug 31, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//------------------------------------------------------------------------------
// <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.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Transform;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3134
{
using System.Threading.Tasks;
[TestFixture]
public class ManyToManyChildFetchByCodeFixtureAsync : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.AddMapping<AMap>();
mapper.AddMapping<BMap>();

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var a1 = new A();
a1.Bs.Add(new B());
a1.Bs.Add(new B());
session.Save(a1);

var a2 = new A();
a2.Bs.Add(new B());
a2.Bs.Add(new B());
session.Save(a2);

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

transaction.Commit();
}
}

[Test]
public async Task ChildFetchQueryOverAsync()
{
using (var session = OpenSession())
{
session.QueryOver<B>().Future();

session.QueryOver<B>()
.Fetch(SelectMode.ChildFetch, b => b)
.Fetch(SelectMode.Fetch, b => b.As)
.Future();

var result = (await (session.QueryOver<B>()
.Fetch(SelectMode.ChildFetch, b => b, b => b.As)
.Fetch(SelectMode.Fetch, b => b.As.First().Bs)
.TransformUsing(Transformers.DistinctRootEntity)
.Future()
.GetEnumerableAsync()))
.ToList();

Assert.That(result.Count, Is.EqualTo(4));
Assert.That(NHibernateUtil.IsInitialized(result[0].As), Is.True);
Assert.That(NHibernateUtil.IsInitialized(result[0].As.First().Bs), Is.True);
Assert.That(result[0].As.Count, Is.EqualTo(1));
Assert.That(result[0].As.First().Bs.Count, Is.EqualTo(2));
}
}
}
}
12 changes: 12 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3134/A.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH3134
{
public class A
{
public virtual Guid Id { get; set; }
public virtual string NameA { get; set; }
public virtual ISet<B> Bs { get; set; } = new HashSet<B>();
}
}
24 changes: 24 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3134/AMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;

namespace NHibernate.Test.NHSpecificTest.GH3134
{
public class AMap : ClassMapping<A>
{
public AMap()
{
this.Table("A");
this.Id(x => x.Id, mapper => { mapper.Column("IDA"); mapper.Generator(Generators.Guid); });
Property(x => x.NameA);
this.Set(
x => x.Bs,
collectionMapping =>
{
collectionMapping.Table("AB");
collectionMapping.Key(k => { k.Column("A"); k.ForeignKey("none");});
collectionMapping.Cascade(Mapping.ByCode.Cascade.All);
},
map => map.ManyToMany(m => { m.Column("B"); }));
}
}
}
12 changes: 12 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3134/B.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH3134
{
public class B
{
public virtual Guid Id { get; set; }
public virtual string NameB { get; set; }
public virtual ISet<A> As { get; set; } = new HashSet<A>();
}
}
24 changes: 24 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3134/BMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;

namespace NHibernate.Test.NHSpecificTest.GH3134
{
public class BMap : ClassMapping<B>
{
public BMap()
{
this.Table("B");
this.Id(x => x.Id, mapper => { mapper.Column("IDB"); mapper.Generator(Generators.Guid); });
Property(x => x.NameB);
this.Set(
x => x.As,
collectionMapping =>
{
collectionMapping.Table("AB");
collectionMapping.Key(k => { k.Column("B"); k.ForeignKey("none"); });
collectionMapping.Cascade(Mapping.ByCode.Cascade.All);
},
map => map.ManyToMany(m => { m.Column("A"); }));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Transform;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3134
{
[TestFixture]
public class ManyToManyChildFetchByCodeFixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.AddMapping<AMap>();
mapper.AddMapping<BMap>();

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var a1 = new A();
a1.Bs.Add(new B());
a1.Bs.Add(new B());
session.Save(a1);

var a2 = new A();
a2.Bs.Add(new B());
a2.Bs.Add(new B());
session.Save(a2);

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

transaction.Commit();
}
}

[Test]
public void ChildFetchQueryOver()
{
using (var session = OpenSession())
{
session.QueryOver<B>().Future();

session.QueryOver<B>()
.Fetch(SelectMode.ChildFetch, b => b)
.Fetch(SelectMode.Fetch, b => b.As)
.Future();

var result = session.QueryOver<B>()
.Fetch(SelectMode.ChildFetch, b => b, b => b.As)
.Fetch(SelectMode.Fetch, b => b.As.First().Bs)
.TransformUsing(Transformers.DistinctRootEntity)
.Future()
.GetEnumerable()
.ToList();

Assert.That(result.Count, Is.EqualTo(4));
Assert.That(NHibernateUtil.IsInitialized(result[0].As), Is.True);
Assert.That(NHibernateUtil.IsInitialized(result[0].As.First().Bs), Is.True);
Assert.That(result[0].As.Count, Is.EqualTo(1));
Assert.That(result[0].As.First().Bs.Count, Is.EqualTo(2));
}
}
}
}
3 changes: 3 additions & 0 deletions src/NHibernate/Loader/OuterJoinableAssociation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ internal string GetSelectFragment(string entitySuffix, string collectionSuffix,
IncludeLazyProps = SelectMode == SelectMode.FetchLazyProperties,
});
case SelectMode.ChildFetch:
// Skip ChildFetch for many-to-many as element id is added by element persister.
if (Joinable.IsCollection && ((IQueryableCollection) Joinable).IsManyToMany)
return string.Empty;
return ReflectHelper.CastOrThrow<ISupportSelectModeJoinable>(Joinable, "child fetch select mode")
.IdentifierSelectFragment(RHSAlias, entitySuffix);

Expand Down