Skip to content

Fix property type retrieval for Composite User Type in LINQ #2877

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 6 commits into from
Aug 9, 2021
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
55 changes: 55 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2856/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//------------------------------------------------------------------------------
// <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.GH2856
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnSetUp()
{
using (var s = Sfi.OpenSession())
using (var t = s.BeginTransaction())
{
s.Save(new Entity {Name = "Company", Phone = new PhoneNumber("745-555-1234"),});
s.Save(new Entity {Name = "Bob", Phone = new PhoneNumber("745-555-1234", "x123"),});
s.Save(new Entity {Name = "Jane", Phone = new PhoneNumber("745-555-1235"),});
s.Flush();
t.Commit();
}
}

protected override void OnTearDown()
{
using (var s = Sfi.OpenSession())
using (var t = s.BeginTransaction())
{
s.Delete("from Entity");
s.Flush();
t.Commit();
}
}

[Test]
public async Task CanDistinctOnCompositeUserTypeAsync()
{
using (var s = OpenSession())
{
var numbers = await (s.Query<Entity>().Select(x => x.Phone.Ext).Distinct().ToListAsync());
Assert.That(numbers.Count, Is.EqualTo(2));
}
}
}
}
9 changes: 9 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2856/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace NHibernate.Test.NHSpecificTest.GH2856
{
public class Entity
{
public int? Id { get; set; }
public string Name { get; set; }
public PhoneNumber Phone { get; set; }
}
}
43 changes: 43 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2856/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Linq;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2856
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (var s = Sfi.OpenSession())
using (var t = s.BeginTransaction())
{
s.Save(new Entity {Name = "Company", Phone = new PhoneNumber("745-555-1234"),});
s.Save(new Entity {Name = "Bob", Phone = new PhoneNumber("745-555-1234", "x123"),});
s.Save(new Entity {Name = "Jane", Phone = new PhoneNumber("745-555-1235"),});
s.Flush();
t.Commit();
}
}

protected override void OnTearDown()
{
using (var s = Sfi.OpenSession())
using (var t = s.BeginTransaction())
{
s.Delete("from Entity");
s.Flush();
t.Commit();
}
}

[Test]
public void CanDistinctOnCompositeUserType()
{
using (var s = OpenSession())
{
var numbers = s.Query<Entity>().Select(x => x.Phone.Ext).Distinct().ToList();
Assert.That(numbers.Count, Is.EqualTo(2));
}
}
}
}
13 changes: 13 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2856/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.GH2856">
<class name="Entity" table="a" lazy="false" optimistic-lock="dirty" dynamic-update="true">
<id name="Id" column="id" unsaved-value="null">
<generator class="native" />
</id>
<property name="Name"/>
<property name="Phone" type="NHibernate.Test.NHSpecificTest.GH2856.PhoneNumberUserType, NHibernate.Test">
<column name="PhoneNumber"/>
<column name="PhoneExt"/>
</property>
</class>
</hibernate-mapping>
15 changes: 15 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2856/PhoneNumber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace NHibernate.Test.NHSpecificTest.GH2856
{
public class PhoneNumber
{
public PhoneNumber(string number, string ext = null)
{
Number = number;
Ext = ext;
}

public string Number { get; }

public string Ext { get; }
}
}
102 changes: 102 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2856/PhoneNumberUserType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Data.Common;
using NHibernate.Engine;
using NHibernate.Type;
using NHibernate.UserTypes;

namespace NHibernate.Test.NHSpecificTest.GH2856
{
class PhoneNumberUserType : ICompositeUserType
{
public string[] PropertyNames
{
get { return new[] {"Number", "Ext"}; }
}

public IType[] PropertyTypes
{
get { return new IType[] {NHibernateUtil.String, NHibernateUtil.String}; }
}

public object GetPropertyValue(object component, int property)
{
PhoneNumber phone = (PhoneNumber) component;

switch (property)
{
case 0: return phone.Number;
case 1: return phone.Ext;
default: throw new NotImplementedException();
}
}

public void SetPropertyValue(object component, int property, object value)
{
throw new NotImplementedException();
}

public System.Type ReturnedClass
{
get { return typeof(PhoneNumber); }
}

public new bool Equals(object x, object y)
{
if (ReferenceEquals(x, null) && ReferenceEquals(y, null))
return true;

if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
return false;

return x.Equals(y);
}

public int GetHashCode(object x)
{
return x.GetHashCode();
}

public object NullSafeGet(DbDataReader dr, string[] names, ISessionImplementor session, object owner)
{
return dr.IsDBNull(dr.GetOrdinal(names[0]))
? null
: (object) new PhoneNumber(
(string) NHibernateUtil.String.NullSafeGet(dr, names[0], session, owner),
(string) NHibernateUtil.String.NullSafeGet(dr, names[1], session, owner));
}

public void NullSafeSet(DbCommand cmd, object value, int index, bool[] settable, ISessionImplementor session)
{
object number = value == null ? null : ((PhoneNumber) value).Number;
object ext = value == null ? null : ((PhoneNumber) value).Ext;

if (settable[0]) NHibernateUtil.String.NullSafeSet(cmd, number, index++, session);
if (settable[1]) NHibernateUtil.String.NullSafeSet(cmd, ext, index, session);
}

public object DeepCopy(object value)
{
return value;
}

public bool IsMutable
{
get { return false; }
}

public object Disassemble(object value, ISessionImplementor session)
{
return value;
}

public object Assemble(object cached, ISessionImplementor session, object owner)
{
return cached;
}

public object Replace(object original, object target, ISessionImplementor session, object owner)
{
return original;
}
}
}
15 changes: 3 additions & 12 deletions src/NHibernate/Util/ExpressionsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,23 +367,14 @@ private static bool TraverseMembers(
break;
case IAbstractComponentType componentType:
currentComponentType = componentType;
if (currentEntityPersister == null)
currentType = TryGetComponentPropertyType(componentType, member.Path);
if (currentEntityPersister != null)
{
// When persister is not available (q.OneToManyCompositeElement[0].Prop), try to get the type from the component
currentType = TryGetComponentPropertyType(componentType, member.Path);
}
else
{
// Concatenate the component property path in order to be able to use EntityMetamodel.GetPropertyType to retrieve the type.
Copy link
Member Author

@bahusoid bahusoid Jul 27, 2021

Choose a reason for hiding this comment

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

EntityMetamodel also can't handle ICompositeUserType components. So why bother to use it at all if we can retrieve all info from componentType

This comment was marked as resolved.

This comment was marked as resolved.

// As GetPropertyType supports only components, do not concatenate when dealing with collection composite elements or elements.
// q.Component.Prop
member = new MemberMetadata(
$"{memberPath}.{member.Path}",
memberPath + "." + member.Path,
member.ConvertType,
member.HasIndexer);

// q.Component.Prop
currentType = currentEntityPersister.EntityMetamodel.GetPropertyType(member.Path);
}

break;
Expand Down