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 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,17 @@
using System.Collections.Generic;
using NHibernate.Test.NHSpecificTest.NH3845.Interfaces;

namespace NHibernate.Test.NHSpecificTest.NH3845.Concrete
{
public class AnotherEntity : IAnotherEntity
{
private ISet<IPropertyEntityC> _childEntities = new HashSet<IPropertyEntityC>();
public virtual int AnotherEntityId { get; set; }
public virtual string Text { get; set; }
public virtual ISet<IPropertyEntityC> ChildEntities
{
get { return _childEntities; }
protected set { _childEntities = value ?? new HashSet<IPropertyEntityC>(); }
}
}
}
26 changes: 26 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3845/Concrete/MainEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using NHibernate.Test.NHSpecificTest.NH3845.Interfaces;

namespace NHibernate.Test.NHSpecificTest.NH3845.Concrete
{
public class MainEntity : IMainEntity
{
private ISet<IPropertyEntityBase> _properties = new HashSet<IPropertyEntityBase>();
private ISet<ISeparateEntity> _separateEntities = new HashSet<ISeparateEntity>();

public virtual int MainEntityId { get; set; }
public virtual string Text { get; set; }

public virtual ISet<IPropertyEntityBase> Properties
{
get { return _properties; }
protected set { _properties = value ?? new HashSet<IPropertyEntityBase>(); }
}

public virtual ISet<ISeparateEntity> SeparateEntities
{
get { return _separateEntities; }
protected set { _separateEntities = value ?? 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,11 @@
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; }
public virtual IAnotherEntity AnotherEntity { 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; }
}
}
226 changes: 226 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3845/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
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"
};

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

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);
}
}

[Test]
public void SourceTypeOfNonPolymorphicEntityPropertyIsCorrect()
{
using (ISession session = OpenSession())
using (session.BeginTransaction())
{
var entityQuery = session.Query<IMainEntity>();
var result =
entityQuery.Where(
m =>
m.Properties.OfType<IPropertyEntityC>().Select(c => c.AnotherEntity).Any(ae => ae.Text == "Another Entity Text"))
.ToList();
Assert.AreEqual(1, result.Count);
}
}

[Test]
public void EnsureParameterValuesAreNotCached()
{
using (ISession session = OpenSession())
using (session.BeginTransaction())
{
var firstEntityQuery = session.Query<IMainEntity>();
var name = "Name B";
var firstResult =
firstEntityQuery.Where(m => m.Properties.OfType<IPropertyEntityB>().Any(p => p.Name == name)).ToList();
Assert.AreEqual(1, firstResult.Count);
Assert.AreEqual("Main Entity Text", firstResult.First().Text);
var secondEntityQuery = session.Query<IMainEntity>();
name = "Name C";
var secondResult =
secondEntityQuery.Where(m => m.Properties.OfType<IPropertyEntityC>().Any(p => p.Name == name)).ToList();
Assert.AreEqual(1, secondResult.Count);
Assert.AreEqual("Second Entity Text", secondResult.First().Text);
}
}

[Test]
public void EnsureParameterValuesInSeparateSessionsAreNotCached()
{
var name = "Name B";
using (ISession session = OpenSession())
using (session.BeginTransaction())
{
var firstEntityQuery = session.Query<IMainEntity>();
var firstResult =
firstEntityQuery.Where(m => m.Properties.OfType<IPropertyEntityB>().Any(p => p.Name == name)).ToList();
Assert.AreEqual(1, firstResult.Count);
Assert.AreEqual("Main Entity Text", firstResult.First().Text);
}
name = "Name C";
using (ISession session = OpenSession())
using (session.BeginTransaction())
{
var secondEntityQuery = session.Query<IMainEntity>();
var secondResult =
secondEntityQuery.Where(m => m.Properties.OfType<IPropertyEntityC>().Any(p => p.Name == name)).ToList();
Assert.AreEqual(1, secondResult.Count);
Assert.AreEqual("Second Entity Text", secondResult.First().Text);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.NH3845.Interfaces
{
public interface IAnotherEntity
{
int AnotherEntityId { get; set; }
string Text { get; set; }
ISet<IPropertyEntityC> ChildEntities { get; }
}
}
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,8 @@
namespace NHibernate.Test.NHSpecificTest.NH3845.Interfaces
{
public interface IPropertyEntityC : IPropertyEntityBase, IHasDescription
{
int AnotherNumber { get; set; }
IAnotherEntity AnotherEntity { get; set; }
}
}
Loading