Skip to content

Commit ac39173

Browse files
Support formula on one-to-many map-key
Fix a null reference exception in OneToManyPersister Spotted while testing #1759
1 parent af319b6 commit ac39173

File tree

5 files changed

+198
-1
lines changed

5 files changed

+198
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
using NUnit.Framework;
15+
using NHibernate.Linq;
16+
17+
namespace NHibernate.Test.NHSpecificTest.GH1760
18+
{
19+
using System.Threading.Tasks;
20+
[TestFixture]
21+
public class MapKeyFormulasFixtureAsync : BugTestCase
22+
{
23+
protected override void OnSetUp()
24+
{
25+
using (var session = OpenSession())
26+
using (var transaction = session.BeginTransaction())
27+
{
28+
var bob =
29+
new User
30+
{
31+
Name = "Bob"
32+
};
33+
session.Save(bob);
34+
35+
var sally =
36+
new User
37+
{
38+
Name = "Sally"
39+
};
40+
session.Save(sally);
41+
42+
var g1 = new Group { Name = "Salesperson" };
43+
session.Save(g1);
44+
45+
g1.UsersByName.Add(bob.Name, bob);
46+
g1.UsersByName.Add(sally.Name, sally);
47+
48+
transaction.Commit();
49+
}
50+
}
51+
52+
protected override void OnTearDown()
53+
{
54+
using (var session = OpenSession())
55+
using (var transaction = session.BeginTransaction())
56+
{
57+
// HQL "delete from" does not handle foreign key order.
58+
session.Delete("from User");
59+
session.CreateQuery("delete System.Object").ExecuteUpdate();
60+
61+
transaction.Commit();
62+
}
63+
}
64+
65+
[Test]
66+
public async Task CheckMapKeyAsync()
67+
{
68+
using (var session = OpenSession())
69+
using (var tx = session.BeginTransaction())
70+
{
71+
var salesperson = await (session.Query<Group>().SingleAsync(u => u.Name == "Salesperson"));
72+
73+
Assert.That(
74+
salesperson.UsersByName,
75+
Has.Count.EqualTo(2)
76+
.And.One.Property(nameof(KeyValuePair<string, User>.Key)).EqualTo("Bob")
77+
.And.One.Property(nameof(KeyValuePair<string, User>.Key)).EqualTo("Sally"));
78+
79+
await (tx.CommitAsync());
80+
}
81+
}
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH1760
5+
{
6+
class User
7+
{
8+
public virtual Guid Id { get; set; }
9+
public virtual string Name { get; set; }
10+
}
11+
12+
class Group
13+
{
14+
public virtual Guid Id { get; set; }
15+
public virtual string Name { get; set; }
16+
17+
public virtual IDictionary<string, User> UsersByName { get; set; } = new Dictionary<string, User>();
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Test.NHSpecificTest.GH1760
7+
{
8+
[TestFixture]
9+
public class MapKeyFormulasFixture : BugTestCase
10+
{
11+
protected override void OnSetUp()
12+
{
13+
using (var session = OpenSession())
14+
using (var transaction = session.BeginTransaction())
15+
{
16+
var bob =
17+
new User
18+
{
19+
Name = "Bob"
20+
};
21+
session.Save(bob);
22+
23+
var sally =
24+
new User
25+
{
26+
Name = "Sally"
27+
};
28+
session.Save(sally);
29+
30+
var g1 = new Group { Name = "Salesperson" };
31+
session.Save(g1);
32+
33+
g1.UsersByName.Add(bob.Name, bob);
34+
g1.UsersByName.Add(sally.Name, sally);
35+
36+
transaction.Commit();
37+
}
38+
}
39+
40+
protected override void OnTearDown()
41+
{
42+
using (var session = OpenSession())
43+
using (var transaction = session.BeginTransaction())
44+
{
45+
// HQL "delete from" does not handle foreign key order.
46+
session.Delete("from User");
47+
session.CreateQuery("delete System.Object").ExecuteUpdate();
48+
49+
transaction.Commit();
50+
}
51+
}
52+
53+
[Test]
54+
public void CheckMapKey()
55+
{
56+
using (var session = OpenSession())
57+
using (var tx = session.BeginTransaction())
58+
{
59+
var salesperson = session.Query<Group>().Single(u => u.Name == "Salesperson");
60+
61+
Assert.That(
62+
salesperson.UsersByName,
63+
Has.Count.EqualTo(2)
64+
.And.One.Property(nameof(KeyValuePair<string, User>.Key)).EqualTo("Bob")
65+
.And.One.Property(nameof(KeyValuePair<string, User>.Key)).EqualTo("Sally"));
66+
67+
tx.Commit();
68+
}
69+
}
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.GH1760">
4+
5+
<class name="User" table="`User`">
6+
<id name="Id" generator="guid.comb"/>
7+
<property name="Name"/>
8+
</class>
9+
10+
<class name="Group" table="`Group`">
11+
<id name="Id" generator="guid.comb"/>
12+
13+
<property name="Name"/>
14+
15+
<map name="UsersByName">
16+
<key column="GroupId"/>
17+
<map-key type="string">
18+
<formula>Name</formula>
19+
</map-key>
20+
<one-to-many class="User"/>
21+
</map>
22+
</class>
23+
24+
</hibernate-mapping>

src/NHibernate/Persister/Collection/OneToManyPersister.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected override SqlCommandInfo GenerateDeleteString()
7676
update.SetJoin(ownerPersister.TableName, KeyColumnNames, KeyType, JoinColumnNames, ownerPersister.GetPropertyColumnNames(CollectionType.LHSPropertyName));
7777
}
7878

79-
if (HasIndex)
79+
if (HasIndex && !indexContainsFormula)
8080
update.AddColumns(IndexColumnNames, "null");
8181

8282
if (HasWhere)

0 commit comments

Comments
 (0)