Skip to content

Commit 648c82a

Browse files
committed
Fix TryGetEntityName for custom entity names
1 parent c90fa14 commit 648c82a

File tree

8 files changed

+520
-149
lines changed

8 files changed

+520
-149
lines changed

src/NHibernate.Test/Async/TypedManyToOne/TypedManyToOneTest.cs

Lines changed: 75 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99

1010

1111
using System.Collections;
12+
using System.Linq;
1213
using NHibernate.Dialect;
1314
using NUnit.Framework;
15+
using NHibernate.Linq;
1416

1517
namespace NHibernate.Test.TypedManyToOne
1618
{
1719
using System.Threading.Tasks;
20+
using System.Threading;
1821
[TestFixture]
1922
public class TypedManyToOneTestAsync : TestCase
2023
{
@@ -35,38 +38,27 @@ protected override bool AppliesTo(Dialect.Dialect dialect)
3538
}
3639

3740
[Test]
38-
public async Task TestCreateQueryAsync()
41+
public async Task TestLinqEntityNameQueryAsync()
3942
{
40-
var cust = new Customer();
41-
cust.CustomerId = "abc123";
42-
cust.Name = "Matt";
43-
44-
var ship = new Address();
45-
ship.Street = "peachtree rd";
46-
ship.State = "GA";
47-
ship.City = "ATL";
48-
ship.Zip = "30326";
49-
ship.AddressId = new AddressId("SHIPPING", "xyz123");
50-
ship.Customer = cust;
51-
52-
var bill = new Address();
53-
bill.Street = "peachtree rd";
54-
bill.State = "GA";
55-
bill.City = "ATL";
56-
bill.Zip = "30326";
57-
bill.AddressId = new AddressId("BILLING", "xyz123");
58-
bill.Customer = cust;
59-
60-
cust.BillingAddress = bill;
61-
cust.ShippingAddress = ship;
62-
63-
using (ISession s = Sfi.OpenSession())
64-
using (ITransaction t = s.BeginTransaction())
43+
var cust = await (CreateCustomerAsync());
44+
using (var s = Sfi.OpenSession())
45+
using (var t = s.BeginTransaction())
6546
{
66-
await (s.PersistAsync(cust));
47+
var billingNotes = await (s.Query<Customer>().Select(o => o.BillingAddress.BillingNotes).FirstAsync());
48+
Assert.That(billingNotes, Is.EqualTo("BillingNotes"));
49+
var shippingNotes = await (s.Query<Customer>().Select(o => o.ShippingAddress.ShippingNotes).FirstAsync());
50+
Assert.That(shippingNotes, Is.EqualTo("ShippingNotes"));
51+
6752
await (t.CommitAsync());
6853
}
6954

55+
await (DeleteCustomerAsync(cust));
56+
}
57+
58+
[Test]
59+
public async Task TestCreateQueryAsync()
60+
{
61+
var cust = await (CreateCustomerAsync());
7062
using (ISession s = Sfi.OpenSession())
7163
using (ITransaction t = s.BeginTransaction())
7264
{
@@ -82,20 +74,7 @@ public async Task TestCreateQueryAsync()
8274
await (t.CommitAsync());
8375
}
8476

85-
using (ISession s = Sfi.OpenSession())
86-
using (ITransaction t = s.BeginTransaction())
87-
{
88-
await (s.SaveOrUpdateAsync(cust));
89-
ship = cust.ShippingAddress;
90-
cust.ShippingAddress = null;
91-
await (s.DeleteAsync("ShippingAddress", ship));
92-
await (s.FlushAsync());
93-
94-
Assert.That(await (s.GetAsync("ShippingAddress", ship.AddressId)), Is.Null);
95-
await (s.DeleteAsync(cust));
96-
97-
await (t.CommitAsync());
98-
}
77+
await (DeleteCustomerAsync(cust));
9978
}
10079

10180
[Test]
@@ -124,5 +103,60 @@ public async Task TestCreateQueryNullAsync()
124103
await (t.CommitAsync());
125104
}
126105
}
106+
107+
private async Task<Customer> CreateCustomerAsync(CancellationToken cancellationToken = default(CancellationToken))
108+
{
109+
var cust = new Customer();
110+
cust.CustomerId = "abc123";
111+
cust.Name = "Matt";
112+
113+
var ship = new Address();
114+
ship.Street = "peachtree rd";
115+
ship.State = "GA";
116+
ship.City = "ATL";
117+
ship.Zip = "30326";
118+
ship.AddressId = new AddressId("SHIPPING", "xyz123");
119+
ship.Customer = cust;
120+
ship.ShippingNotes = "ShippingNotes";
121+
122+
var bill = new Address();
123+
bill.Street = "peachtree rd";
124+
bill.State = "GA";
125+
bill.City = "ATL";
126+
bill.Zip = "30326";
127+
bill.AddressId = new AddressId("BILLING", "xyz123");
128+
bill.Customer = cust;
129+
bill.BillingNotes = "BillingNotes";
130+
131+
cust.BillingAddress = bill;
132+
cust.ShippingAddress = ship;
133+
134+
using (ISession s = Sfi.OpenSession())
135+
using (ITransaction t = s.BeginTransaction())
136+
{
137+
await (s.PersistAsync(cust, cancellationToken));
138+
await (t.CommitAsync(cancellationToken));
139+
}
140+
141+
return cust;
142+
}
143+
144+
private async Task DeleteCustomerAsync(Customer cust, CancellationToken cancellationToken = default(CancellationToken))
145+
{
146+
using (var s = Sfi.OpenSession())
147+
using (var t = s.BeginTransaction())
148+
{
149+
await (s.SaveOrUpdateAsync(cust, cancellationToken));
150+
var ship = cust.ShippingAddress;
151+
cust.ShippingAddress = null;
152+
await (s.DeleteAsync("ShippingAddress", ship, cancellationToken));
153+
await (s.FlushAsync(cancellationToken));
154+
155+
Assert.That(await (s.GetAsync("ShippingAddress", ship.AddressId, cancellationToken)), Is.Null);
156+
await (s.DeleteAsync(cust, cancellationToken));
157+
158+
await (t.CommitAsync(cancellationToken));
159+
}
160+
}
127161
}
128162
}

src/NHibernate.Test/TypedManyToOne/Address.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace NHibernate.Test.TypedManyToOne
44
{
5-
[Serializable]
6-
public class Address
7-
{
8-
public virtual AddressId AddressId {get; set;}
9-
public virtual string Street { get; set; }
10-
public virtual string City { get; set; }
11-
public virtual string State { get; set; }
12-
public virtual string Zip { get; set; }
13-
public virtual Customer Customer { get; set; }
14-
}
5+
[Serializable]
6+
public class Address
7+
{
8+
public virtual AddressId AddressId { get; set; }
9+
public virtual string Street { get; set; }
10+
public virtual string City { get; set; }
11+
public virtual string State { get; set; }
12+
public virtual string Zip { get; set; }
13+
public virtual Customer Customer { get; set; }
14+
public virtual string BillingNotes { get; set; }
15+
public virtual string ShippingNotes { get; set; }
16+
}
1517
}

src/NHibernate.Test/TypedManyToOne/Customer.hbm.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<property name="City" not-null="true"/>
5454
<property name="State" not-null="true"/>
5555
<property name="Zip" not-null="true"/>
56-
56+
<property name="BillingNotes" />
5757
</class>
5858

5959
<class name="Address"
@@ -73,7 +73,7 @@
7373
<property name="City" not-null="true"/>
7474
<property name="State" not-null="true"/>
7575
<property name="Zip" not-null="true"/>
76-
76+
<property name="ShippingNotes" />
7777
</class>
7878

7979
</hibernate-mapping>

src/NHibernate.Test/TypedManyToOne/TypedManyToOneTest.cs

Lines changed: 73 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using System.Linq;
23
using NHibernate.Dialect;
34
using NUnit.Framework;
45

@@ -24,38 +25,27 @@ protected override bool AppliesTo(Dialect.Dialect dialect)
2425
}
2526

2627
[Test]
27-
public void TestCreateQuery()
28+
public void TestLinqEntityNameQuery()
2829
{
29-
var cust = new Customer();
30-
cust.CustomerId = "abc123";
31-
cust.Name = "Matt";
32-
33-
var ship = new Address();
34-
ship.Street = "peachtree rd";
35-
ship.State = "GA";
36-
ship.City = "ATL";
37-
ship.Zip = "30326";
38-
ship.AddressId = new AddressId("SHIPPING", "xyz123");
39-
ship.Customer = cust;
40-
41-
var bill = new Address();
42-
bill.Street = "peachtree rd";
43-
bill.State = "GA";
44-
bill.City = "ATL";
45-
bill.Zip = "30326";
46-
bill.AddressId = new AddressId("BILLING", "xyz123");
47-
bill.Customer = cust;
48-
49-
cust.BillingAddress = bill;
50-
cust.ShippingAddress = ship;
51-
52-
using (ISession s = Sfi.OpenSession())
53-
using (ITransaction t = s.BeginTransaction())
30+
var cust = CreateCustomer();
31+
using (var s = Sfi.OpenSession())
32+
using (var t = s.BeginTransaction())
5433
{
55-
s.Persist(cust);
34+
var billingNotes = s.Query<Customer>().Select(o => o.BillingAddress.BillingNotes).First();
35+
Assert.That(billingNotes, Is.EqualTo("BillingNotes"));
36+
var shippingNotes = s.Query<Customer>().Select(o => o.ShippingAddress.ShippingNotes).First();
37+
Assert.That(shippingNotes, Is.EqualTo("ShippingNotes"));
38+
5639
t.Commit();
5740
}
5841

42+
DeleteCustomer(cust);
43+
}
44+
45+
[Test]
46+
public void TestCreateQuery()
47+
{
48+
var cust = CreateCustomer();
5949
using (ISession s = Sfi.OpenSession())
6050
using (ITransaction t = s.BeginTransaction())
6151
{
@@ -71,20 +61,7 @@ public void TestCreateQuery()
7161
t.Commit();
7262
}
7363

74-
using (ISession s = Sfi.OpenSession())
75-
using (ITransaction t = s.BeginTransaction())
76-
{
77-
s.SaveOrUpdate(cust);
78-
ship = cust.ShippingAddress;
79-
cust.ShippingAddress = null;
80-
s.Delete("ShippingAddress", ship);
81-
s.Flush();
82-
83-
Assert.That(s.Get("ShippingAddress", ship.AddressId), Is.Null);
84-
s.Delete(cust);
85-
86-
t.Commit();
87-
}
64+
DeleteCustomer(cust);
8865
}
8966

9067
[Test]
@@ -113,5 +90,60 @@ public void TestCreateQueryNull()
11390
t.Commit();
11491
}
11592
}
93+
94+
private Customer CreateCustomer()
95+
{
96+
var cust = new Customer();
97+
cust.CustomerId = "abc123";
98+
cust.Name = "Matt";
99+
100+
var ship = new Address();
101+
ship.Street = "peachtree rd";
102+
ship.State = "GA";
103+
ship.City = "ATL";
104+
ship.Zip = "30326";
105+
ship.AddressId = new AddressId("SHIPPING", "xyz123");
106+
ship.Customer = cust;
107+
ship.ShippingNotes = "ShippingNotes";
108+
109+
var bill = new Address();
110+
bill.Street = "peachtree rd";
111+
bill.State = "GA";
112+
bill.City = "ATL";
113+
bill.Zip = "30326";
114+
bill.AddressId = new AddressId("BILLING", "xyz123");
115+
bill.Customer = cust;
116+
bill.BillingNotes = "BillingNotes";
117+
118+
cust.BillingAddress = bill;
119+
cust.ShippingAddress = ship;
120+
121+
using (ISession s = Sfi.OpenSession())
122+
using (ITransaction t = s.BeginTransaction())
123+
{
124+
s.Persist(cust);
125+
t.Commit();
126+
}
127+
128+
return cust;
129+
}
130+
131+
private void DeleteCustomer(Customer cust)
132+
{
133+
using (var s = Sfi.OpenSession())
134+
using (var t = s.BeginTransaction())
135+
{
136+
s.SaveOrUpdate(cust);
137+
var ship = cust.ShippingAddress;
138+
cust.ShippingAddress = null;
139+
s.Delete("ShippingAddress", ship);
140+
s.Flush();
141+
142+
Assert.That(s.Get("ShippingAddress", ship.AddressId), Is.Null);
143+
s.Delete(cust);
144+
145+
t.Commit();
146+
}
147+
}
116148
}
117149
}

src/NHibernate/Linq/Functions/ListIndexerGenerator.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,30 @@ namespace NHibernate.Linq.Functions
1212
{
1313
internal class ListIndexerGenerator : BaseHqlGeneratorForMethod,IRuntimeMethodHqlGenerator
1414
{
15+
private static readonly HashSet<MethodInfo> _supportedMethods = new HashSet<MethodInfo>
16+
{
17+
ReflectHelper.GetMethodDefinition(() => Enumerable.ElementAt<object>(null, 0)),
18+
ReflectHelper.GetMethodDefinition(() => Queryable.ElementAt<object>(null, 0))
19+
};
20+
1521
public ListIndexerGenerator()
1622
{
17-
SupportedMethods = new[]
18-
{
19-
ReflectHelper.GetMethodDefinition(() => Enumerable.ElementAt<object>(null, 0)),
20-
ReflectHelper.GetMethodDefinition(() => Queryable.ElementAt<object>(null, 0))
21-
};
23+
SupportedMethods = _supportedMethods;
2224
}
2325

2426
public bool SupportsMethod(MethodInfo method)
2527
{
26-
return method != null &&
27-
method.Name == "get_Item" &&
28-
(method.IsMethodOf(typeof(IList)) || method.IsMethodOf(typeof(IList<>)));
28+
return IsRuntimeMethodSupported(method);
29+
}
30+
31+
public static bool IsMethodSupported(MethodInfo method)
32+
{
33+
if (method.IsGenericMethod)
34+
{
35+
method = method.GetGenericMethodDefinition();
36+
}
37+
38+
return _supportedMethods.Contains(method) || IsRuntimeMethodSupported(method);
2939
}
3040

3141
public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)
@@ -40,5 +50,12 @@ public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
4050

4151
return treeBuilder.Index(collection, index);
4252
}
53+
54+
private static bool IsRuntimeMethodSupported(MethodInfo method)
55+
{
56+
return method != null &&
57+
method.Name == "get_Item" &&
58+
(method.IsMethodOf(typeof(IList)) || method.IsMethodOf(typeof(IList<>)));
59+
}
4360
}
44-
}
61+
}

0 commit comments

Comments
 (0)