Skip to content

Fix #1994 (many-to-many filter in hql) #1995

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 10 commits into from
Jun 9, 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
2 changes: 1 addition & 1 deletion src/NHibernate.Test/Async/NHSpecificTest/GH0000/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected override void OnTearDown()
// 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
// instead if in need of having NHibernate ordering the deletes, but this will cause
// loading the entities in the session.
session.CreateQuery("delete from System.Object").ExecuteUpdate();

Expand Down
117 changes: 117 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1994/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//------------------------------------------------------------------------------
// <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.Dialect;
using NHibernate.Linq;
using NHibernate.Transform;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1994
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var a = new Asset();
a.Documents.Add(new Document { IsDeleted = true });
a.Documents.Add(new Document { IsDeleted = false });

session.Save(a);
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 NHibernate ordering the deletes, but this will cause
// loading the entities in the session.

session.Delete("from System.Object");

transaction.Commit();
}
}

[Test]
public async Task TestUnfilteredLinqQueryAsync()
{
using (var s = OpenSession())
{
var query = await (s.Query<Asset>()
.FetchMany(x => x.Documents)
.ToListAsync());

Assert.That(query.Count, Is.EqualTo(1), "unfiltered assets");
Assert.That(query[0].Documents.Count, Is.EqualTo(2), "unfiltered asset documents");
}
}

[Test]
public async Task TestFilteredByWhereCollectionLinqQueryAsync()
{
if(Dialect is PostgreSQLDialect)
Assert.Ignore("Dialect doesn't support 0/1 to bool implicit cast");

using (var s = OpenSession())
{
var query = await (s.Query<Asset>()
.FetchMany(x => x.DocumentsFiltered)
.ToListAsync());

Assert.That(query.Count, Is.EqualTo(1), "unfiltered assets");
Assert.That(query[0].DocumentsFiltered.Count, Is.EqualTo(1), "unfiltered asset documents");
}
}

[Test]
public async Task TestFilteredLinqQueryAsync()
{
using (var s = OpenSession())
{
s.EnableFilter("deletedFilter").SetParameter("deletedParam", false);
var query = await (s.Query<Asset>()
.FetchMany(x => x.Documents)
.ToListAsync());

Assert.That(query.Count, Is.EqualTo(1), "filtered assets");
Assert.That(query[0].Documents.Count, Is.EqualTo(1), "filtered asset documents");
}
}

[Test]
public async Task TestFilteredQueryOverAsync()
{
using (var s = OpenSession())
{
s.EnableFilter("deletedFilter").SetParameter("deletedParam", false);

var query = await (s.QueryOver<Asset>()
.Fetch(SelectMode.Fetch, x => x.Documents)
.TransformUsing(Transformers.DistinctRootEntity)
.ListAsync<Asset>());

Assert.That(query.Count, Is.EqualTo(1), "filtered assets");
Assert.That(query[0].Documents.Count, Is.EqualTo(1), "filtered asset documents");
}
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHSpecificTest/GH0000/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override void OnTearDown()
// 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
// instead if in need of having NHibernate ordering the deletes, but this will cause
// loading the entities in the session.
session.CreateQuery("delete from System.Object").ExecuteUpdate();

Expand Down
23 changes: 23 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1994/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH1994
{
public class Base
{
public virtual Guid Key { get; set; }

public virtual bool IsDeleted { get; set; }
}

public class Asset : Base
{
public virtual ISet<Document> Documents { get; set; } = new HashSet<Document>();
public virtual ISet<Document> DocumentsFiltered { get; set; } = new HashSet<Document>();
}

public class Document : Base
{
public virtual ISet<Asset> Assets { get; set; } = new HashSet<Asset>();
}
}
106 changes: 106 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1994/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System.Linq;
using NHibernate.Dialect;
using NHibernate.Linq;
using NHibernate.Transform;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1994
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var a = new Asset();
a.Documents.Add(new Document { IsDeleted = true });
a.Documents.Add(new Document { IsDeleted = false });

session.Save(a);
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 NHibernate ordering the deletes, but this will cause
// loading the entities in the session.

session.Delete("from System.Object");

transaction.Commit();
}
}

[Test]
public void TestUnfilteredLinqQuery()
{
using (var s = OpenSession())
{
var query = s.Query<Asset>()
.FetchMany(x => x.Documents)
.ToList();

Assert.That(query.Count, Is.EqualTo(1), "unfiltered assets");
Assert.That(query[0].Documents.Count, Is.EqualTo(2), "unfiltered asset documents");
}
}

[Test]
public void TestFilteredByWhereCollectionLinqQuery()
{
if(Dialect is PostgreSQLDialect)
Assert.Ignore("Dialect doesn't support 0/1 to bool implicit cast");

using (var s = OpenSession())
{
var query = s.Query<Asset>()
.FetchMany(x => x.DocumentsFiltered)
.ToList();

Assert.That(query.Count, Is.EqualTo(1), "unfiltered assets");
Assert.That(query[0].DocumentsFiltered.Count, Is.EqualTo(1), "unfiltered asset documents");
}
}

[Test]
public void TestFilteredLinqQuery()
{
using (var s = OpenSession())
{
s.EnableFilter("deletedFilter").SetParameter("deletedParam", false);
var query = s.Query<Asset>()
.FetchMany(x => x.Documents)
.ToList();

Assert.That(query.Count, Is.EqualTo(1), "filtered assets");
Assert.That(query[0].Documents.Count, Is.EqualTo(1), "filtered asset documents");
}
}

[Test]
public void TestFilteredQueryOver()
{
using (var s = OpenSession())
{
s.EnableFilter("deletedFilter").SetParameter("deletedParam", false);

var query = s.QueryOver<Asset>()
.Fetch(SelectMode.Fetch, x => x.Documents)
.TransformUsing(Transformers.DistinctRootEntity)
.List<Asset>();

Assert.That(query.Count, Is.EqualTo(1), "filtered assets");
Assert.That(query[0].Documents.Count, Is.EqualTo(1), "filtered asset documents");
}
}
}
}
39 changes: 39 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1994/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH1994">

<class name="Base" abstract="true" dynamic-update="true" lazy="true">
<id name="Key" column="pdoid" generator="guid.comb"/>

<property name="IsDeleted" column="IsDeleted" />

<filter name="deletedFilter" condition="IsDeleted = :deletedParam"/>

</class>

<union-subclass name="Asset" table="Asset" lazy="true" extends="Base">
<set name="Documents" table="asset_to_document" lazy="true" cascade="all-delete-orphan">
<key column="AssetId"/>
<many-to-many class="Document" column="DocumentId">
<filter name="deletedFilter" condition="IsDeleted = :deletedParam"/>
</many-to-many>
</set>
<set name="DocumentsFiltered" table="asset_to_document" lazy="true" cascade="none">
<key column="AssetId"/>
<many-to-many class="Document" column="DocumentId" where="IsDeleted = 0"/>
</set>
</union-subclass>

<union-subclass name="Document" table="Document" lazy="true" extends="Base">

<set name="Assets" table="asset_to_document" lazy="true" cascade="all-delete-orphan">
<key column="DocumentId"/>
<many-to-many class="Asset" column="AssetId"/>
</set>
</union-subclass>

<filter-def name="deletedFilter">
<filter-param name="deletedParam" type="bool"/>
</filter-def>

</hibernate-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace NHibernate.Persister.Collection
{
using System.Threading.Tasks;
using System.Threading;

public abstract partial class AbstractCollectionPersister : ICollectionMetadata, ISqlLoadableCollection,
IPostInsertIdentityPersister, ISupportSelectModeJoinable, ICompositeKeyPostInsertIdentityPersister
{
Expand Down
8 changes: 7 additions & 1 deletion src/NHibernate/Loader/BasicLoader.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Persister.Collection;
using NHibernate.Persister.Entity;
Expand Down Expand Up @@ -50,7 +51,7 @@ protected override void PostInstantiate()
{
bagCount++;
}
collectionDescriptors[i] = new GeneratedCollectionAliases(collectionPersisters[i], collectionSuffixes[i]);
collectionDescriptors[i] = new GeneratedCollectionAliases(GetCollectionUserProvidedAlias(i), collectionPersisters[i], collectionSuffixes[i]);
}
}
else
Expand All @@ -66,6 +67,11 @@ protected override void PostInstantiate()
}
}

protected virtual IDictionary<string, string[]> GetCollectionUserProvidedAlias(int index)
{
return null;
}

private static bool IsBag(ICollectionPersister collectionPersister)
{
var type = collectionPersister.CollectionType.GetType();
Expand Down
Loading