Skip to content

Fix criteria collection ordering #1997

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 2 commits into from
Jan 31, 2019
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
Expand Up @@ -395,6 +395,23 @@ public void SkipRootEntityIsNotSupportedAsync()
}
}

[Test]
public async Task OrderedInnerJoinFetchAsync()
{
using (var session = OpenSession())
{
var list = await (session.QueryOver<EntityComplex>()
.Where(ec => ec.Id == _parentEntityComplexId)
.JoinQueryOver(c => c.ChildrenList).Fetch(SelectMode.Fetch, child => child)
.TransformUsing(Transformers.DistinctRootEntity)
.ListAsync());

var childList = list[0].ChildrenList;
Assert.That(list[0].ChildrenList.Count, Is.GreaterThan(1));
Assert.That(list[0].ChildrenList, Is.EqualTo(list[0].ChildrenList.OrderByDescending(c => c.OrderIdx)), "wrong order");
}
}

[Test, Obsolete]
public async Task FetchModeEagerForLazyAsync()
{
Expand Down Expand Up @@ -562,15 +579,19 @@ protected override HbmMapping GetMappings()
m.Column("SameTypeChildId");
m.ForeignKey("none");
});
MapList(rc, ep => ep.ChildrenList);
MapList(rc, ep => ep.ChildrenList, mapper: m => m.OrderBy("OrderIdx desc"));
MapList(rc, ep => ep.ChildrenListEmpty);
});

MapSimpleChild(
mapper,
default(EntitySimpleChild),
c => c.Children,
rc => { rc.Property(sc => sc.LazyProp, mp => mp.Lazy(true)); });
rc =>
{
rc.Property(sc => sc.LazyProp, mp => mp.Lazy(true));
rc.Property(sc => sc.OrderIdx);
});
MapSimpleChild(mapper, default(Level2Child), c => c.Children);
MapSimpleChild<Level3Child>(mapper);

Expand Down Expand Up @@ -598,7 +619,7 @@ private static void MapSimpleChild<TChild, TSubChild>(ModelMapper mapper, TChild
});
}

private static void MapList<TParent, TElement>(IClassMapper<TParent> rc, Expression<Func<TParent, IEnumerable<TElement>>> expression, CollectionFetchMode fetchMode = null) where TParent : class
private static void MapList<TParent, TElement>(IClassMapper<TParent> rc, Expression<Func<TParent, IEnumerable<TElement>>> expression, CollectionFetchMode fetchMode = null, Action<IBagPropertiesMapper<TParent, TElement>> mapper = null) where TParent : class
{
rc.Bag(
expression,
Expand All @@ -620,6 +641,7 @@ private static void MapList<TParent, TElement>(IClassMapper<TParent> rc, Express
{
m.Fetch(fetchMode);
}
mapper?.Invoke(m);
},
a => a.OneToMany());
}
Expand Down Expand Up @@ -682,7 +704,8 @@ protected override void OnSetUp()
},
}
}
}
},
OrderIdx = 100
};

var child2 = new EntitySimpleChild
Expand All @@ -691,6 +714,17 @@ protected override void OnSetUp()
LazyProp = "LazyFromSimpleChild2",
};

var child3 = new EntitySimpleChild
{
Name = "Child3",
OrderIdx = 0
};
var child4 = new EntitySimpleChild
{
Name = "Child4",
OrderIdx = 50
};

var parent = new EntityComplex
{
Name = "ComplexEntityParent",
Expand All @@ -701,7 +735,7 @@ protected override void OnSetUp()
{
Name = "ComplexEntityChild"
},
ChildrenList = new List<EntitySimpleChild> {child1},
ChildrenList = new List<EntitySimpleChild> {child3, child1, child4 },
ChildrenListEmpty = new List<EntityComplex> { },
};
session.Save(new EntityEager()
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate.Test/Criteria/SelectModeTest/Entities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class EntitySimpleChild : BaseChild
{
public virtual IList<Level2Child> Children { get; set; } = new List<Level2Child>();
public virtual string LazyProp { get; set; }
public virtual int OrderIdx { get; set; }
}

public class Level2Child : BaseChild
Expand Down
44 changes: 39 additions & 5 deletions src/NHibernate.Test/Criteria/SelectModeTest/SelectModeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,23 @@ public void SkipRootEntityIsNotSupported()
}
}

[Test]
public void OrderedInnerJoinFetch()
{
using (var session = OpenSession())
{
var list = session.QueryOver<EntityComplex>()
.Where(ec => ec.Id == _parentEntityComplexId)
.JoinQueryOver(c => c.ChildrenList).Fetch(SelectMode.Fetch, child => child)
.TransformUsing(Transformers.DistinctRootEntity)
.List();

var childList = list[0].ChildrenList;
Assert.That(list[0].ChildrenList.Count, Is.GreaterThan(1));
Assert.That(list[0].ChildrenList, Is.EqualTo(list[0].ChildrenList.OrderByDescending(c => c.OrderIdx)), "wrong order");
}
}

[Test, Obsolete]
public void FetchModeEagerForLazy()
{
Expand Down Expand Up @@ -593,15 +610,19 @@ protected override HbmMapping GetMappings()
m.Column("SameTypeChildId");
m.ForeignKey("none");
});
MapList(rc, ep => ep.ChildrenList);
MapList(rc, ep => ep.ChildrenList, mapper: m => m.OrderBy("OrderIdx desc"));
MapList(rc, ep => ep.ChildrenListEmpty);
});

MapSimpleChild(
mapper,
default(EntitySimpleChild),
c => c.Children,
rc => { rc.Property(sc => sc.LazyProp, mp => mp.Lazy(true)); });
rc =>
{
rc.Property(sc => sc.LazyProp, mp => mp.Lazy(true));
rc.Property(sc => sc.OrderIdx);
});
MapSimpleChild(mapper, default(Level2Child), c => c.Children);
MapSimpleChild<Level3Child>(mapper);

Expand Down Expand Up @@ -629,7 +650,7 @@ private static void MapSimpleChild<TChild, TSubChild>(ModelMapper mapper, TChild
});
}

private static void MapList<TParent, TElement>(IClassMapper<TParent> rc, Expression<Func<TParent, IEnumerable<TElement>>> expression, CollectionFetchMode fetchMode = null) where TParent : class
private static void MapList<TParent, TElement>(IClassMapper<TParent> rc, Expression<Func<TParent, IEnumerable<TElement>>> expression, CollectionFetchMode fetchMode = null, Action<IBagPropertiesMapper<TParent, TElement>> mapper = null) where TParent : class
{
rc.Bag(
expression,
Expand All @@ -651,6 +672,7 @@ private static void MapList<TParent, TElement>(IClassMapper<TParent> rc, Express
{
m.Fetch(fetchMode);
}
mapper?.Invoke(m);
},
a => a.OneToMany());
}
Expand Down Expand Up @@ -713,7 +735,8 @@ protected override void OnSetUp()
},
}
}
}
},
OrderIdx = 100
};

var child2 = new EntitySimpleChild
Expand All @@ -722,6 +745,17 @@ protected override void OnSetUp()
LazyProp = "LazyFromSimpleChild2",
};

var child3 = new EntitySimpleChild
{
Name = "Child3",
OrderIdx = 0
};
var child4 = new EntitySimpleChild
{
Name = "Child4",
OrderIdx = 50
};

var parent = new EntityComplex
{
Name = "ComplexEntityParent",
Expand All @@ -732,7 +766,7 @@ protected override void OnSetUp()
{
Name = "ComplexEntityChild"
},
ChildrenList = new List<EntitySimpleChild> {child1},
ChildrenList = new List<EntitySimpleChild> {child3, child1, child4 },
ChildrenListEmpty = new List<EntityComplex> { },
};
session.Save(new EntityEager()
Expand Down
36 changes: 15 additions & 21 deletions src/NHibernate/Loader/JoinWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -882,32 +882,26 @@ protected SqlString OrderBy(IList<OuterJoinableAssociation> associations)
OuterJoinableAssociation last = null;
foreach (OuterJoinableAssociation oj in associations)
{
if (oj.JoinType == JoinType.LeftOuterJoin)
if (oj.ShouldFetchCollectionPersister())
{
if (oj.Joinable.IsCollection)
IQueryableCollection queryableCollection = (IQueryableCollection) oj.Joinable;
if (queryableCollection.HasOrdering)
{
IQueryableCollection queryableCollection = (IQueryableCollection)oj.Joinable;
if (queryableCollection.HasOrdering)
{
string orderByString = queryableCollection.GetSQLOrderByString(oj.RHSAlias);
buf.Add(orderByString).Add(StringHelper.CommaSpace);
}
string orderByString = queryableCollection.GetSQLOrderByString(oj.RHSAlias);
buf.Add(orderByString).Add(StringHelper.CommaSpace);
}
else
}
else if (!oj.IsCollection && last?.ShouldFetchCollectionPersister() == true)
{
// it might still need to apply a collection ordering based on a
// many-to-many defined order-by...
IQueryableCollection queryableCollection = (IQueryableCollection) last.Joinable;
if (queryableCollection.IsManyToMany && last.IsManyToManyWith(oj))
{
// it might still need to apply a collection ordering based on a
// many-to-many defined order-by...
if (last != null && last.Joinable.IsCollection)
if (queryableCollection.HasManyToManyOrdering)
{
IQueryableCollection queryableCollection = (IQueryableCollection)last.Joinable;
if (queryableCollection.IsManyToMany && last.IsManyToManyWith(oj))
{
if (queryableCollection.HasManyToManyOrdering)
{
string orderByString = queryableCollection.GetManyToManyOrderByString(oj.RHSAlias);
buf.Add(orderByString).Add(StringHelper.CommaSpace);
}
}
string orderByString = queryableCollection.GetManyToManyOrderByString(oj.RHSAlias);
buf.Add(orderByString).Add(StringHelper.CommaSpace);
}
}
}
Expand Down