Skip to content

Commit 638db27

Browse files
authored
Enhance nullability check for "==" and "!=" operators for LINQ provider (#1996)
1 parent 0d65c8b commit 638db27

23 files changed

+1357
-31
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.DomainModel.Northwind.Entities
4+
{
5+
public class AnotherEntityRequired
6+
{
7+
public virtual int Id { get; set; }
8+
9+
public virtual string Output { get; set; }
10+
11+
public virtual string Input { get; set; }
12+
13+
public virtual Address Address { get; set; }
14+
15+
public virtual AnotherEntityNullability InputNullability { get; set; }
16+
17+
public virtual string NullableOutput { get; set; }
18+
19+
public virtual AnotherEntityRequired NullableAnotherEntityRequired { get; set; }
20+
21+
public virtual int? NullableAnotherEntityRequiredId { get; set; }
22+
23+
public virtual ISet<AnotherEntity> RelatedItems { get; set; } = new HashSet<AnotherEntity>();
24+
25+
public virtual bool? NullableBool { get; set; }
26+
}
27+
28+
public enum AnotherEntityNullability
29+
{
30+
False = 0,
31+
True = 1
32+
}
33+
}

src/NHibernate.DomainModel/Northwind/Entities/User.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public interface IUser
2626
Role Role { get; set; }
2727
EnumStoredAsString Enum1 { get; set; }
2828
EnumStoredAsInt32 Enum2 { get; set; }
29+
IUser CreatedBy { get; set; }
30+
IUser ModifiedBy { get; set; }
2931
}
3032

3133
public class User : IUser, IEntity
@@ -50,6 +52,10 @@ public class User : IUser, IEntity
5052

5153
public virtual EnumStoredAsInt32 Enum2 { get; set; }
5254

55+
public virtual IUser CreatedBy { get; set; }
56+
57+
public virtual IUser ModifiedBy { get; set; }
58+
5359
public virtual int NotMapped { get; set; }
5460

5561
public virtual Role NotMappedRole { get; set; }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernate.DomainModel.Northwind.Entities" assembly="NHibernate.DomainModel">
3+
<class name="AnotherEntityRequired" table="AnotherEntity" mutable="false" schema-action="none">
4+
<id name="Id">
5+
<generator class="native" />
6+
</id>
7+
<property name="Output" not-null="true" />
8+
<property name="Input" not-null="true" />
9+
<property name="NullableOutput" formula="Output" lazy="true" />
10+
<property name="InputNullability" formula="case when Input is not null then 0 else 1 end" lazy="true" />
11+
<property name="NullableAnotherEntityRequiredId" formula="Id" lazy="true" />
12+
<property name="NullableBool" formula="null" lazy="true" />
13+
<component name="Address" insert="false" update="false" lazy="true">
14+
<property name="Street" formula="Input" access="field.camelcase-underscore" />
15+
<property name="City" formula="Output" access="field.camelcase-underscore" />
16+
</component>
17+
<many-to-one name="NullableAnotherEntityRequired" formula="Id" />
18+
<set name="RelatedItems" lazy="true" inverse="true">
19+
<key column="Id"/>
20+
<one-to-many class="AnotherEntity"/>
21+
</set>
22+
</class>
23+
</hibernate-mapping>

src/NHibernate.DomainModel/Northwind/Mappings/User.hbm.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@
77
<generator class="assigned" />
88
</id>
99

10-
<property name="Name" type="AnsiString" />
10+
<property name="Name" type="AnsiString" not-null="true" />
1111
<property name="InvalidLoginAttempts" type="Int32" />
1212
<property name="RegisteredAt" type="DateTime" />
1313
<property name="LastLoginDate" type="DateTime" />
1414

15+
<many-to-one name="CreatedBy" class="User" not-null="true" lazy="false">
16+
<column name="CreatedById" not-null="true" />
17+
</many-to-one>
18+
19+
<many-to-one name="ModifiedBy" class="User" lazy="false">
20+
<column name="ModifiedById" />
21+
</many-to-one>
22+
1523
<property name="Enum1" type="NHibernate.DomainModel.Northwind.Entities.EnumStoredAsStringType, NHibernate.DomainModel">
1624
<column name="Enum1" length="12" />
1725
</property>

src/NHibernate.Test/Async/Linq/NullComparisonTests.cs

Lines changed: 455 additions & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.

src/NHibernate.Test/DbScripts/MsSql2012DialectLinqReadonlyCreateScript.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3774,6 +3774,8 @@ CREATE TABLE [dbo].[Users](
37743774
[Property1] [varchar](255) NULL,
37753775
[Property2] [varchar](255) NULL,
37763776
[OtherProperty1] [varchar](255) NULL,
3777+
[CreatedById] [int] NOT NULL,
3778+
[ModifiedById] [int] NULL
37773779
PRIMARY KEY CLUSTERED
37783780
(
37793781
[UserId] ASC
@@ -3783,9 +3785,9 @@ GO
37833785
SET ANSI_PADDING OFF
37843786
GO
37853787
SET IDENTITY_INSERT [dbo].[Users] ON
3786-
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')
3787-
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)
3788-
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)
3788+
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)
3789+
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)
3790+
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)
37893791
SET IDENTITY_INSERT [dbo].[Users] OFF
37903792
/****** Object: Table [dbo].[TimeSheetUsers] Script Date: 06/17/2010 13:08:54 ******/
37913793
SET ANSI_NULLS ON
Binary file not shown.

src/NHibernate.Test/Linq/LinqTestCase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected override string[] Mappings
2929
"Northwind.Mappings.Supplier.hbm.xml",
3030
"Northwind.Mappings.Territory.hbm.xml",
3131
"Northwind.Mappings.AnotherEntity.hbm.xml",
32+
"Northwind.Mappings.AnotherEntityRequired.hbm.xml",
3233
"Northwind.Mappings.Role.hbm.xml",
3334
"Northwind.Mappings.User.hbm.xml",
3435
"Northwind.Mappings.TimeSheet.hbm.xml",
@@ -69,4 +70,4 @@ public static void AssertByIds<TEntity, TId>(IEnumerable<TEntity> entities, TId[
6970
Assert.That(entities.Select(x => entityIdGetter(x)), Is.EquivalentTo(expectedIds));
7071
}
7172
}
72-
}
73+
}

src/NHibernate.Test/Linq/NorthwindDbCreator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public static void CreateMiscTestData(ISession session)
7070
}
7171
};
7272

73+
foreach (var user in users)
74+
{
75+
user.CreatedBy = users[0];
76+
}
77+
7378
var timesheets = new[]
7479
{
7580
new Timesheet
@@ -3707,4 +3712,4 @@ static void CreateOrderLines22(IStatelessSession session, IDictionary<int, Order
37073712
orderLine = new OrderLine { Order = ordersById[11077], Product = productsByName["Original Frankfurter grüne Soße"], UnitPrice = 13.00M, Quantity = 2, Discount = 0M }; session.Insert(orderLine);
37083713
}
37093714
}
3710-
}
3715+
}

0 commit comments

Comments
 (0)