Skip to content

NH-3845 - Make OfType work better with polymorphic mappings #456

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3845/Concrete/MainEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using NHibernate.Test.NHSpecificTest.NH3845.Interfaces;

namespace NHibernate.Test.NHSpecificTest.NH3845.Concrete
{
public class MainEntity : IMainEntity
{
public virtual int MainEntityId { get; set; }
public virtual string Text { get; set; }
public virtual ISet<IPropertyEntityBase> Properties { get; protected set; } = new HashSet<IPropertyEntityBase>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C# 6 is not yet supported by our build engine, please change to using C#5

public virtual ISet<ISeparateEntity> SeparateEntities { get; protected set; } = new HashSet<ISeparateEntity>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using NHibernate.Test.NHSpecificTest.NH3845.Interfaces;

namespace NHibernate.Test.NHSpecificTest.NH3845.Concrete
{
public class PropertyEntityA : PropertyEntityBase, IPropertyEntityA
{
public virtual int SerialNumber { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using NHibernate.Test.NHSpecificTest.NH3845.Interfaces;

namespace NHibernate.Test.NHSpecificTest.NH3845.Concrete
{
public class PropertyEntityB : PropertyEntityBase, IPropertyEntityB
{
public virtual string Description { get; set; }
public virtual string AnotherString { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using NHibernate.Test.NHSpecificTest.NH3845.Interfaces;

namespace NHibernate.Test.NHSpecificTest.NH3845.Concrete
{
public abstract class PropertyEntityBase : IPropertyEntityBase
{
public virtual int PropertyEntityBaseId { get; set; }
public virtual string Name { get; set; }
public virtual string SharedValue { get; set; }
public virtual IMainEntity MainEntity { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using NHibernate.Test.NHSpecificTest.NH3845.Interfaces;

namespace NHibernate.Test.NHSpecificTest.NH3845.Concrete
{
public class PropertyEntityC : PropertyEntityBase, IPropertyEntityC
{
public virtual string Description { get; set; }
public virtual int AnotherNumber { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using NHibernate.Test.NHSpecificTest.NH3845.Interfaces;

namespace NHibernate.Test.NHSpecificTest.NH3845.Concrete
{
public class SeparateEntity : ISeparateEntity
{
public virtual int SeparateEntityId { get; set; }
public virtual IMainEntity MainEntity { get; set; }
public virtual int SeparateEntityValue { get; set; }
}
}
159 changes: 159 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3845/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using System.Linq;
using NHibernate.Linq;
using NHibernate.Test.NHSpecificTest.NH3845.Concrete;
using NHibernate.Test.NHSpecificTest.NH3845.Interfaces;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3845
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (ISession session = OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
var entityA = new PropertyEntityA()
{
Name = "Name A",
SerialNumber = 4321,
SharedValue = "Some Value"
};
var entityB = new PropertyEntityB()
{
Name = "Name B",
Description = "Some Description",
SharedValue = "Another Value",
AnotherString = "Another String"
};
var entityC = new PropertyEntityC()
{
Name = "Name C",
Description = "Has Description",
SharedValue = "Value",
AnotherNumber = 42
};

var separateEntity = new SeparateEntity()
{
SeparateEntityValue = 5432
};

var mainEntity = new MainEntity()
{
Text = "Main Entity Text"
};
var secondMainEntity = new MainEntity()
{
Text = "Second Entity Text"
};
session.Save(mainEntity);
session.Save(secondMainEntity);
entityA.MainEntity = mainEntity;
entityB.MainEntity = mainEntity;
entityC.MainEntity = secondMainEntity;
separateEntity.MainEntity = secondMainEntity;
session.Save(entityA);
session.Save(entityB);
session.Save(entityC);
session.Save(separateEntity);

//mainEntity.Properties.Add(entityA);
//mainEntity.Properties.Add(entityB);
session.Flush();
transaction.Commit();
}
}

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

session.Flush();
transaction.Commit();
}
}

[Test]
public void OfTypeWorksWithSingleEntityInterface()
{
using (ISession session = OpenSession())
using (session.BeginTransaction())
{
var entityQuery = session.Query<IMainEntity>();
var result =
entityQuery.Where(m => m.Properties.OfType<IPropertyEntityB>().Any()).ToList();
Assert.AreEqual(1, result.Count);
}
}

[Test]
public void OfTypeWorksWithUnrelatedInterface()
{
using (ISession session = OpenSession())
using (session.BeginTransaction())
{
var entityQuery = session.Query<IMainEntity>();
var result =
entityQuery.Where(m => m.Properties.OfType<IHasDescription>().Any()).ToList();
Assert.AreEqual(2, result.Count);
}
}

[Test]
public void CanQueryOfTypePropertyWithUnrelatedInterface()
{
using (ISession session = OpenSession())
using (session.BeginTransaction())
{
var entityQuery = session.Query<IMainEntity>();
var result =
entityQuery.Where(m => m.Properties.OfType<IHasDescription>().Any(d => d.Description == "Has Description"))
.ToList();
Assert.AreEqual(1, result.Count);
}
}

[Test]
public void ImpossibleOfTypeReturnsNoResults()
{
using (ISession session = OpenSession())
using (session.BeginTransaction())
{
var entityQuery = session.Query<IMainEntity>();
var result =
entityQuery.Where(m => m.Properties.OfType<ISession>().Any()).ToList();
Assert.IsEmpty(result);
}
}

[Test]
public void ImpossibleMappedOfTypeReturnsNoResults()
{
using (ISession session = OpenSession())
using (session.BeginTransaction())
{
var entityQuery = session.Query<IMainEntity>();
var result =
entityQuery.Where(m => m.Properties.OfType<ISeparateEntity>().Any(se => se.SeparateEntityValue == 5432)).ToList();
Assert.IsEmpty(result);
}
}

[Test]
public void OfTypeAppliedToNonSubclassEntityStillWorks()
{
using (ISession session = OpenSession())
using (session.BeginTransaction())
{
var entityQuery = session.Query<IMainEntity>();
var result = entityQuery.Where(m => m.SeparateEntities.OfType<SeparateEntity>().Any()).ToList();
Assert.AreEqual(1, result.Count);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NHibernate.Test.NHSpecificTest.NH3845.Interfaces
{
public interface IHasDescription
{
string Description { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.NH3845.Interfaces
{
public interface IMainEntity
{
int MainEntityId { get; set; }
string Text { get; set; }
ISet<IPropertyEntityBase> Properties { get; }
ISet<ISeparateEntity> SeparateEntities { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NHibernate.Test.NHSpecificTest.NH3845.Interfaces
{
public interface IPropertyEntityA : IPropertyEntityBase
{
int SerialNumber { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NHibernate.Test.NHSpecificTest.NH3845.Interfaces
{
public interface IPropertyEntityB : IPropertyEntityBase, IHasDescription
{
string AnotherString { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace NHibernate.Test.NHSpecificTest.NH3845.Interfaces
{
public interface IPropertyEntityBase
{
int PropertyEntityBaseId { get; set; }
string Name { get; set; }
string SharedValue { get; set; }
IMainEntity MainEntity { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NHibernate.Test.NHSpecificTest.NH3845.Interfaces
{
public interface IPropertyEntityC : IPropertyEntityBase, IHasDescription
{
int AnotherNumber { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace NHibernate.Test.NHSpecificTest.NH3845.Interfaces
{
public interface ISeparateEntity
{
int SeparateEntityId { get; set; }
IMainEntity MainEntity { get; set; }
int SeparateEntityValue { get; set; }
}
}
45 changes: 45 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3845/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH3845.Concrete">
<class name="MainEntity">
<id name="MainEntityId" type="Int32">
<generator class="native" />
</id>
<property name="Text" />
<set name="Properties" cascade="all" inverse="true" lazy="true">
<key column="MainEntityId" />
<one-to-many class="PropertyEntityBase" />
</set>
<set name="SeparateEntities" cascade="all" inverse="true" lazy="true">
<key column="MainEntityId" />
<one-to-many class="SeparateEntity" />
</set>
</class>
<class name="PropertyEntityBase" abstract="true">
<id name="PropertyEntityBaseId" type="Int32">
<generator class="enhanced-sequence" />
</id>
<property name="Name" />
<property name="SharedValue" />
<many-to-one name="MainEntity" class="MainEntity" cascade="all" column="MainEntityId" />
<union-subclass name="PropertyEntityA">
<property name="SerialNumber" />
</union-subclass>
<union-subclass name="PropertyEntityB">
<property name="Description" />
<property name="AnotherString" />
</union-subclass>
<union-subclass name="PropertyEntityC">
<property name="Description" />
<property name="AnotherNumber" />
</union-subclass>
</class>
<class name="SeparateEntity">
<id name="SeparateEntityId" type="Int32">
<generator class="native" />
</id>
<many-to-one name="MainEntity" class="MainEntity" cascade="all" column="MainEntityId" />
<property name="SeparateEntityValue" />
</class>
</hibernate-mapping>
15 changes: 15 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,13 @@
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Fixture.cs" />
<Compile Include="Linq\ByMethod\DistinctTests.cs" />
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
<Compile Include="NHSpecificTest\NH3845\Concrete\MainEntity.cs" />
<Compile Include="NHSpecificTest\NH3845\Concrete\PropertyEntityA.cs" />
<Compile Include="NHSpecificTest\NH3845\Concrete\PropertyEntityB.cs" />
<Compile Include="NHSpecificTest\NH3845\Concrete\PropertyEntityBase.cs" />
<Compile Include="NHSpecificTest\NH3845\Concrete\PropertyEntityC.cs" />
<Compile Include="NHSpecificTest\NH3845\Concrete\SeparateEntity.cs" />
<Compile Include="NHSpecificTest\NH3845\Fixture.cs" />
<Compile Include="NHSpecificTest\NH2931\Fixture.cs" />
<Compile Include="NHSpecificTest\NH2931\Mappings.cs" />
<Compile Include="NHSpecificTest\NH2931\Models.cs" />
Expand Down Expand Up @@ -1280,6 +1287,13 @@
<Compile Include="NHSpecificTest\NH3641\TestFixture.cs" />
<Compile Include="NHSpecificTest\NH3800\Domain.cs" />
<Compile Include="NHSpecificTest\NH3800\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3845\Interfaces\IHasDescription.cs" />
<Compile Include="NHSpecificTest\NH3845\Interfaces\IMainEntity.cs" />
<Compile Include="NHSpecificTest\NH3845\Interfaces\IPropertyEntityA.cs" />
<Compile Include="NHSpecificTest\NH3845\Interfaces\IPropertyEntityB.cs" />
<Compile Include="NHSpecificTest\NH3845\Interfaces\IPropertyEntityBase.cs" />
<Compile Include="NHSpecificTest\NH3845\Interfaces\IPropertyEntityC.cs" />
<Compile Include="NHSpecificTest\NH3845\Interfaces\ISeparateEntity.cs" />
<Compile Include="NHSpecificTest\Properties\CompositePropertyRefTest.cs" />
<Compile Include="NHSpecificTest\Properties\DynamicEntityTest.cs" />
<Compile Include="NHSpecificTest\Properties\Model.cs" />
Expand Down Expand Up @@ -3160,6 +3174,7 @@
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="NHSpecificTest\NH3845\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3518\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3609\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3818\Mappings.hbm.xml" />
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Hql/Ast/HqlTreeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public HqlFalse False()
return new HqlFalse(_factory);
}

public HqlIn In(HqlExpression itemExpression, HqlTreeNode source)
public HqlIn In(HqlExpression itemExpression, params HqlTreeNode[] source)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've found the proper solution for not changing this. You need to use HqlTreeBuilder.ExpressionSubTreeHolder.

{
return new HqlIn(_factory, itemExpression, source);
}
Expand Down
Loading