Skip to content

Enhance nullability check for "==" and "!=" operators for LINQ provider #1996

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 11 commits into from
Feb 27, 2020
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;

namespace NHibernate.DomainModel.Northwind.Entities
{
public class AnotherEntityRequired
{
public virtual int Id { get; set; }

public virtual string Output { get; set; }

public virtual string Input { get; set; }

public virtual Address Address { get; set; }

public virtual AnotherEntityNullability InputNullability { get; set; }

public virtual string NullableOutput { get; set; }

public virtual AnotherEntityRequired NullableAnotherEntityRequired { get; set; }

public virtual int? NullableAnotherEntityRequiredId { get; set; }

public virtual ISet<AnotherEntity> RelatedItems { get; set; } = new HashSet<AnotherEntity>();

public virtual bool? NullableBool { get; set; }
}

public enum AnotherEntityNullability
{
False = 0,
True = 1
}
}
6 changes: 6 additions & 0 deletions src/NHibernate.DomainModel/Northwind/Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public interface IUser
Role Role { get; set; }
EnumStoredAsString Enum1 { get; set; }
EnumStoredAsInt32 Enum2 { get; set; }
IUser CreatedBy { get; set; }
IUser ModifiedBy { get; set; }
}

public class User : IUser, IEntity
Expand All @@ -50,6 +52,10 @@ public class User : IUser, IEntity

public virtual EnumStoredAsInt32 Enum2 { get; set; }

public virtual IUser CreatedBy { get; set; }

public virtual IUser ModifiedBy { get; set; }

public virtual int NotMapped { get; set; }

public virtual Role NotMappedRole { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernate.DomainModel.Northwind.Entities" assembly="NHibernate.DomainModel">
<class name="AnotherEntityRequired" table="AnotherEntity" mutable="false" schema-action="none">
<id name="Id">
<generator class="native" />
</id>
<property name="Output" not-null="true" />
<property name="Input" not-null="true" />
<property name="NullableOutput" formula="Output" lazy="true" />
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've had to use lazy as otherwise an invalid query is generated due to the missing table alias for the column used in the formula.

<property name="InputNullability" formula="case when Input is not null then 0 else 1 end" lazy="true" />
<property name="NullableAnotherEntityRequiredId" formula="Id" lazy="true" />
<property name="NullableBool" formula="null" lazy="true" />
<component name="Address" insert="false" update="false" lazy="true">
<property name="Street" formula="Input" access="field.camelcase-underscore" />
<property name="City" formula="Output" access="field.camelcase-underscore" />
</component>
<many-to-one name="NullableAnotherEntityRequired" formula="Id" />
<set name="RelatedItems" lazy="true" inverse="true">
<key column="Id"/>
<one-to-many class="AnotherEntity"/>
</set>
</class>
</hibernate-mapping>
10 changes: 9 additions & 1 deletion src/NHibernate.DomainModel/Northwind/Mappings/User.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
<generator class="assigned" />
</id>

<property name="Name" type="AnsiString" />
<property name="Name" type="AnsiString" not-null="true" />
<property name="InvalidLoginAttempts" type="Int32" />
<property name="RegisteredAt" type="DateTime" />
<property name="LastLoginDate" type="DateTime" />

<many-to-one name="CreatedBy" class="User" not-null="true" lazy="false">
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to add lazy="false", otherwise some tests in QueryReuseTests (CanReuseAfterFirst, CanReuseAfterFirstOrDefault, CanReuseAfterSingle, CanReuseAfterSingleOfDefault) would fail.

<column name="CreatedById" not-null="true" />
</many-to-one>

<many-to-one name="ModifiedBy" class="User" lazy="false">
<column name="ModifiedById" />
</many-to-one>

<property name="Enum1" type="NHibernate.DomainModel.Northwind.Entities.EnumStoredAsStringType, NHibernate.DomainModel">
<column name="Enum1" length="12" />
</property>
Expand Down
455 changes: 455 additions & 0 deletions src/NHibernate.Test/Async/Linq/NullComparisonTests.cs

Large diffs are not rendered by default.

Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3774,6 +3774,8 @@ CREATE TABLE [dbo].[Users](
[Property1] [varchar](255) NULL,
[Property2] [varchar](255) NULL,
[OtherProperty1] [varchar](255) NULL,
[CreatedById] [int] NOT NULL,
[ModifiedById] [int] NULL
PRIMARY KEY CLUSTERED
(
[UserId] ASC
Expand All @@ -3783,9 +3785,9 @@ GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Users] ON
INSERT [dbo].[Users] ([UserId], [Name], [InvalidLoginAttempts], [RegisteredAt], [LastLoginDate], [Enum1], [Enum2], [RoleId], [Property1], [Property2], [OtherProperty1]) VALUES (1, N'ayende', 4, CAST(0x00009D9800000000 AS DateTime), NULL, N'Medium', 1, 1, N'test1', N'test2', N'othertest1')
INSERT [dbo].[Users] ([UserId], [Name], [InvalidLoginAttempts], [RegisteredAt], [LastLoginDate], [Enum1], [Enum2], [RoleId], [Property1], [Property2], [OtherProperty1]) VALUES (2, N'rahien', 5, CAST(0x00008D3E00000000 AS DateTime), NULL, N'Small', 0, 2, NULL, N'test2', NULL)
INSERT [dbo].[Users] ([UserId], [Name], [InvalidLoginAttempts], [RegisteredAt], [LastLoginDate], [Enum1], [Enum2], [Features], [RoleId], [Property1], [Property2], [OtherProperty1]) VALUES (3, N'nhibernate', 6, CAST(0x00008EAC00000000 AS DateTime), CAST(0x00009D970110B41C AS DateTime), N'Medium', 0, 8, NULL, NULL, NULL, NULL)
INSERT [dbo].[Users] ([UserId], [Name], [InvalidLoginAttempts], [RegisteredAt], [LastLoginDate], [Enum1], [Enum2], [RoleId], [Property1], [Property2], [OtherProperty1], [CreatedById], [ModifiedById]) VALUES (1, N'ayende', 4, CAST(0x00009D9800000000 AS DateTime), NULL, N'Medium', 1, 1, N'test1', N'test2', N'othertest1', 1, NULL)
INSERT [dbo].[Users] ([UserId], [Name], [InvalidLoginAttempts], [RegisteredAt], [LastLoginDate], [Enum1], [Enum2], [RoleId], [Property1], [Property2], [OtherProperty1], [CreatedById], [ModifiedById]) VALUES (2, N'rahien', 5, CAST(0x00008D3E00000000 AS DateTime), NULL, N'Small', 0, 2, NULL, N'test2', NULL, 1, NULL)
INSERT [dbo].[Users] ([UserId], [Name], [InvalidLoginAttempts], [RegisteredAt], [LastLoginDate], [Enum1], [Enum2], [Features], [RoleId], [Property1], [Property2], [OtherProperty1], [CreatedById], [ModifiedById]) VALUES (3, N'nhibernate', 6, CAST(0x00008EAC00000000 AS DateTime), CAST(0x00009D970110B41C AS DateTime), N'Medium', 0, 8, NULL, NULL, NULL, NULL, 1, NULL)
SET IDENTITY_INSERT [dbo].[Users] OFF
/****** Object: Table [dbo].[TimeSheetUsers] Script Date: 06/17/2010 13:08:54 ******/
SET ANSI_NULLS ON
Expand Down
Binary file not shown.
3 changes: 2 additions & 1 deletion src/NHibernate.Test/Linq/LinqTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected override string[] Mappings
"Northwind.Mappings.Supplier.hbm.xml",
"Northwind.Mappings.Territory.hbm.xml",
"Northwind.Mappings.AnotherEntity.hbm.xml",
"Northwind.Mappings.AnotherEntityRequired.hbm.xml",
"Northwind.Mappings.Role.hbm.xml",
"Northwind.Mappings.User.hbm.xml",
"Northwind.Mappings.TimeSheet.hbm.xml",
Expand Down Expand Up @@ -69,4 +70,4 @@ public static void AssertByIds<TEntity, TId>(IEnumerable<TEntity> entities, TId[
Assert.That(entities.Select(x => entityIdGetter(x)), Is.EquivalentTo(expectedIds));
}
}
}
}
7 changes: 6 additions & 1 deletion src/NHibernate.Test/Linq/NorthwindDbCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public static void CreateMiscTestData(ISession session)
}
};

foreach (var user in users)
{
user.CreatedBy = users[0];
}

var timesheets = new[]
{
new Timesheet
Expand Down Expand Up @@ -3707,4 +3712,4 @@ static void CreateOrderLines22(IStatelessSession session, IDictionary<int, Order
orderLine = new OrderLine { Order = ordersById[11077], Product = productsByName["Original Frankfurter grüne Soße"], UnitPrice = 13.00M, Quantity = 2, Discount = 0M }; session.Insert(orderLine);
}
}
}
}
Loading