Skip to content

Apply filters for entity joins in hql #2550

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
Sep 18, 2020
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
82 changes: 82 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2549/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//------------------------------------------------------------------------------
// <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 NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH2549
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnSetUp()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.Save(new Person {Id = 1, Name = "Name"});
s.Save(new Customer {Deleted = false, Name = "Name", Id = 1});
s.Save(new Customer {Deleted = true, Name = "Name", Id = 2});

t.Commit();
}
}

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

[Test]
public async Task EntityJoinFilterLinqAsync()
{
using (var s = OpenSession())
{
var list = await ((from p in s.Query<Person>()
join c in s.Query<Customer>() on p.Name equals c.Name
select p).ToListAsync());

s.EnableFilter("DeletedCustomer").SetParameter("deleted", false);

var filteredList = await ((from p in s.Query<Person>()
join c in s.Query<Customer>() on p.Name equals c.Name
select p).ToListAsync());

Assert.That(list, Has.Count.EqualTo(2));
Assert.That(filteredList, Has.Count.EqualTo(1));
}
}

[Test]
public async Task EntityJoinFilterQueryOverAsync()
{
using (var s = OpenSession())
{
Customer c = null;
Person p = null;
var list = await (s.QueryOver(() => p).JoinEntityAlias(() => c, () => c.Name == p.Name).ListAsync());

s.EnableFilter("DeletedCustomer").SetParameter("deleted", false);

var filteredList = await (s.QueryOver(() => p).JoinEntityAlias(() => c, () => c.Name == p.Name).ListAsync());

Assert.That(list, Has.Count.EqualTo(2));
Assert.That(filteredList, Has.Count.EqualTo(1));
}
}
}
}
70 changes: 70 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2549/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Linq;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2549
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.Save(new Person {Id = 1, Name = "Name"});
s.Save(new Customer {Deleted = false, Name = "Name", Id = 1});
s.Save(new Customer {Deleted = true, Name = "Name", Id = 2});

t.Commit();
}
}

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

[Test]
public void EntityJoinFilterLinq()
{
using (var s = OpenSession())
{
var list = (from p in s.Query<Person>()
join c in s.Query<Customer>() on p.Name equals c.Name
select p).ToList();

s.EnableFilter("DeletedCustomer").SetParameter("deleted", false);

var filteredList = (from p in s.Query<Person>()
join c in s.Query<Customer>() on p.Name equals c.Name
select p).ToList();

Assert.That(list, Has.Count.EqualTo(2));
Assert.That(filteredList, Has.Count.EqualTo(1));
}
}

[Test]
public void EntityJoinFilterQueryOver()
{
using (var s = OpenSession())
{
Customer c = null;
Person p = null;
var list = s.QueryOver(() => p).JoinEntityAlias(() => c, () => c.Name == p.Name).List();

s.EnableFilter("DeletedCustomer").SetParameter("deleted", false);

var filteredList = s.QueryOver(() => p).JoinEntityAlias(() => c, () => c.Name == p.Name).List();

Assert.That(list, Has.Count.EqualTo(2));
Assert.That(filteredList, Has.Count.EqualTo(1));
}
}
}
}
26 changes: 26 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2549/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH2549" >

<class name="Customer">
<id name="Id">
<generator class="assigned" />
</id>
<property name="Name" />
<property name="Deleted" type="Boolean" not-null="true" />

<filter name="DeletedCustomer" condition="Deleted = :deleted" />
</class>

<class name="Person">
<id name="Id">
<generator class="assigned" />
</id>
<property name="Name" />
</class>

<filter-def name="DeletedCustomer">
<filter-param name="deleted" type="Boolean"/>
</filter-def>

</hibernate-mapping>
15 changes: 15 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2549/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace NHibernate.Test.NHSpecificTest.GH2549
{
public class Customer
{
public virtual int Id { get; set; }
public virtual bool Deleted { get; set; }
public virtual string Name { get; set; }
}

public class Person
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
}
13 changes: 6 additions & 7 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/EntityJoinFromElement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Antlr.Runtime;
using System;
using Antlr.Runtime;
using NHibernate.Engine;
using NHibernate.Persister.Entity;
using NHibernate.SqlCommand;
using NHibernate.Type;
Expand All @@ -15,12 +17,9 @@ public EntityJoinFromElement(FromClause fromClause, IQueryable entityPersister,
EntityType entityType = (EntityType) entityPersister.Type;
InitializeEntity(fromClause, entityPersister.EntityName, entityPersister, entityType, alias, tableAlias);

JoinSequence = new EntityJoinJoinSequenceImpl(
SessionFactoryHelper.Factory,
entityType,
entityPersister.TableName,
tableAlias,
joinType);
//NH Specific: hibernate uses special class EntityJoinJoinSequenceImpl
JoinSequence = new JoinSequence(SessionFactoryHelper.Factory)
.AddJoin(entityType, tableAlias, joinType, Array.Empty<string>());

fromClause.Walker.AddQuerySpaces(entityPersister.QuerySpaces);
}
Expand Down
51 changes: 0 additions & 51 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/EntityJoinJoinSequenceImpl.cs

This file was deleted.