Skip to content

Commit cb89668

Browse files
committed
Support Dynamic BatchFetchStyle
1 parent e0384a3 commit cb89668

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1434
-74
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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 NHibernate.Cfg;
15+
using NHibernate.Cfg.MappingSchema;
16+
using NHibernate.Loader;
17+
using NUnit.Framework;
18+
using Environment = NHibernate.Cfg.Environment;
19+
20+
namespace NHibernate.Test.NHSpecificTest.NH3530
21+
{
22+
using System.Threading.Tasks;
23+
using System.Threading;
24+
//NH-3530 (GH-1316)
25+
[TestFixture]
26+
public class DynamicBatchFetchStyleFixtureAsync : TestCaseMappingByCode
27+
{
28+
private readonly List<object> _ids = new List<object>();
29+
30+
protected override void Configure(Configuration configuration)
31+
{
32+
base.Configure(configuration);
33+
configuration.SetProperty(Environment.BatchFetchStyle, BatchFetchStyle.Dynamic.ToString());
34+
}
35+
36+
[Test]
37+
public async Task CanLoadEntityAsync()
38+
{
39+
await (PrepareEntitiesAsync(2));
40+
41+
using (var session = OpenSession())
42+
{
43+
var proxy = await (session.LoadAsync<Entity>(_ids[0]));
44+
var result = await (session.GetAsync<Entity>(_ids[1]));
45+
46+
Assert.That(result.Name, Is.Not.Null);
47+
48+
var childrenCount = result.Children.Count;
49+
//Assert.That(NHibernateUtil.IsInitialized(proxy), Is.True);
50+
Assert.That(NHibernateUtil.IsInitialized(proxy.Children), Is.True);
51+
Assert.That(childrenCount, Is.EqualTo(4));
52+
}
53+
}
54+
55+
[KnownBug("GH-2960")]
56+
[Test]
57+
public async Task CanLoadComponentEntityAsync()
58+
{
59+
await (PrepareComponentEntitiesAsync(2));
60+
61+
using (var session = OpenSession())
62+
{
63+
var proxy = await (session.LoadAsync<EntityComponent>(_ids[0]));
64+
var result = await (session.GetAsync<EntityComponent>(_ids[1]));
65+
66+
Assert.That(result.Name, Is.Not.Null);
67+
68+
var childrenCount = result.Children.Count;
69+
Assert.That(NHibernateUtil.IsInitialized(proxy.Children), Is.True);
70+
Assert.That(childrenCount, Is.EqualTo(4));
71+
}
72+
}
73+
74+
[Test]
75+
public async Task CanLoadComponentIdEntityAsync()
76+
{
77+
await (PrepareComponentIdEntitiesAsync(2));
78+
79+
using (var session = OpenSession())
80+
{
81+
var proxy = await (session.LoadAsync<EntityComponentId>(_ids[0]));
82+
var result = await (session.GetAsync<EntityComponentId>(_ids[1]));
83+
84+
Assert.That(result.Name, Is.Not.Null);
85+
86+
var childrenCount = result.Children.Count;
87+
Assert.That(NHibernateUtil.IsInitialized(proxy.Children), Is.True);
88+
Assert.That(childrenCount, Is.EqualTo(4));
89+
}
90+
}
91+
92+
[TestCase(1)]
93+
[TestCase(2)]
94+
[TestCase(3)]
95+
[TestCase(4)]
96+
[TestCase(5)]
97+
public async Task CanLoadBatchAsync(int loadCount)
98+
{
99+
await (PrepareEntitiesAsync(5));
100+
101+
using (var session = OpenSession())
102+
{
103+
foreach (var id in _ids.Take(loadCount))
104+
{
105+
await (session.LoadAsync<Entity>(id));
106+
}
107+
108+
var result = await (session.GetAsync<Entity>(_ids[0]));
109+
var result2 = await (session.GetAsync<Entity>(_ids[1]));
110+
var last = await (session.GetAsync<Entity>(_ids.Last()));
111+
112+
var count = result.Children.Count;
113+
Assert.That(result.Name, Is.Not.Null);
114+
Assert.That(last.Name, Is.Not.Null);
115+
Assert.That(NHibernateUtil.IsInitialized(result2.Children), Is.True);
116+
}
117+
}
118+
119+
protected override void OnTearDown()
120+
{
121+
using (var session = OpenSession())
122+
using (var transaction = session.BeginTransaction())
123+
{
124+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
125+
transaction.Commit();
126+
}
127+
}
128+
129+
protected override HbmMapping GetMappings()
130+
{
131+
return EntityMappings.CreateMapping();
132+
}
133+
134+
private async Task PrepareEntitiesAsync(int count, CancellationToken cancellationToken = default(CancellationToken))
135+
{
136+
_ids.Clear();
137+
using (var session = OpenSession())
138+
using (var transaction = session.BeginTransaction())
139+
{
140+
for (int i = 0; i < count; i++)
141+
{
142+
var entity = new Entity { Name = "some name" + 1 };
143+
AddChildren(entity.Children, 4);
144+
_ids.Add((Guid) await (session.SaveAsync(entity, cancellationToken)));
145+
}
146+
147+
await (transaction.CommitAsync(cancellationToken));
148+
}
149+
}
150+
151+
private async Task PrepareComponentEntitiesAsync(int count, CancellationToken cancellationToken = default(CancellationToken))
152+
{
153+
_ids.Clear();
154+
using (var session = OpenSession())
155+
using (var transaction = session.BeginTransaction())
156+
{
157+
for (int i = 0; i < count; i++)
158+
{
159+
var entity = new EntityComponent { Id1 = i, Id2 = i + 1, Name = "some name" + 1 };
160+
AddChildren(entity.Children, 4);
161+
162+
_ids.Add(await (session.SaveAsync(entity, cancellationToken)));
163+
}
164+
165+
await (transaction.CommitAsync(cancellationToken));
166+
}
167+
}
168+
169+
private async Task PrepareComponentIdEntitiesAsync(int count, CancellationToken cancellationToken = default(CancellationToken))
170+
{
171+
_ids.Clear();
172+
using (var session = OpenSession())
173+
using (var transaction = session.BeginTransaction())
174+
{
175+
for (int i = 0; i < count; i++)
176+
{
177+
var entity = new EntityComponentId { Id = new ComponentId { Id1 = i, Id2 = i + 1 }, Name = "some name" + 1 };
178+
AddChildren(entity.Children, 4);
179+
_ids.Add(await (session.SaveAsync(entity, cancellationToken)));
180+
}
181+
182+
await (transaction.CommitAsync(cancellationToken));
183+
}
184+
}
185+
186+
private static void AddChildren<T>(IList<T> list, int count) where T : new()
187+
{
188+
for (int i = 0; i < count; i++)
189+
{
190+
list.Add(new T());
191+
}
192+
}
193+
}
194+
}
195+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH3530
6+
{
7+
public class Entity
8+
{
9+
public virtual Guid Id { get; set; }
10+
public virtual string Name { get; set; }
11+
public virtual IList<Child> Children { get; set; } = new List<Child>();
12+
}
13+
14+
public class EntityComponent
15+
{
16+
public virtual int Id1 { get; set; }
17+
public virtual int Id2 { get; set; }
18+
public virtual string Name { get; set; }
19+
public virtual IList<ChildForComponent> Children { get; set; } = new List<ChildForComponent>();
20+
21+
public override bool Equals(object obj)
22+
{
23+
return obj == this;
24+
}
25+
26+
public override int GetHashCode()
27+
{
28+
return RuntimeHelpers.GetHashCode(this);
29+
}
30+
}
31+
32+
public class ComponentId
33+
{
34+
public int Id1 { get; set; }
35+
public int Id2 { get; set; }
36+
37+
protected bool Equals(ComponentId other)
38+
{
39+
return Id1 == other.Id1 && Id2 == other.Id2;
40+
}
41+
42+
public override bool Equals(object obj)
43+
{
44+
if (ReferenceEquals(null, obj)) return false;
45+
if (ReferenceEquals(this, obj)) return true;
46+
if (obj.GetType() != this.GetType()) return false;
47+
return Equals((ComponentId) obj);
48+
}
49+
50+
public override int GetHashCode()
51+
{
52+
unchecked
53+
{
54+
return (Id1 * 397) ^ Id2;
55+
}
56+
}
57+
}
58+
59+
public class EntityComponentId
60+
{
61+
public virtual ComponentId Id { get; set; }
62+
63+
public virtual string Name { get; set; }
64+
public virtual IList<ChildForComponent> Children { get; set; } = new List<ChildForComponent>();
65+
66+
public override bool Equals(object obj)
67+
{
68+
return obj == this;
69+
}
70+
71+
public override int GetHashCode()
72+
{
73+
return RuntimeHelpers.GetHashCode(this);
74+
}
75+
}
76+
77+
public class Child
78+
{
79+
public virtual Guid Id { get; set; }
80+
public virtual string Name { get; set; }
81+
public virtual Guid ParentId { get; set; }
82+
}
83+
84+
public class ChildForComponent
85+
{
86+
public virtual Guid Id { get; set; }
87+
public virtual string Name { get; set; }
88+
public virtual int ParentId1 { get; set; }
89+
public virtual int ParentId2 { get; set; }
90+
}
91+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using NHibernate.Cfg.MappingSchema;
2+
using NHibernate.Mapping.ByCode;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3530
5+
{
6+
class EntityMappings
7+
{
8+
public static HbmMapping CreateMapping()
9+
{
10+
var mapper = new ModelMapper();
11+
mapper.Class<Entity>(
12+
rc =>
13+
{
14+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
15+
rc.Property(x => x.Name);
16+
rc.BatchSize(3);
17+
rc.Bag(
18+
x => x.Children,
19+
m =>
20+
{
21+
m.Key(
22+
km =>
23+
{
24+
km.Column("ParentId");
25+
km.ForeignKey("none");
26+
});
27+
m.BatchSize(10);
28+
m.Cascade(Mapping.ByCode.Cascade.All);
29+
},
30+
xr => xr.OneToMany());
31+
});
32+
33+
mapper.Class<EntityComponent>(
34+
rc =>
35+
{
36+
rc.ComposedId(
37+
x =>
38+
{
39+
x.Property(t => t.Id1);
40+
x.Property(e => e.Id2);
41+
});
42+
rc.Property(x => x.Name);
43+
rc.BatchSize(3);
44+
rc.Bag(
45+
x => x.Children,
46+
m =>
47+
{
48+
m.Key(
49+
km =>
50+
{
51+
km.Columns(x => x.Name("ParentId1"), x => x.Name("ParentId2"));
52+
km.ForeignKey("none");
53+
});
54+
m.BatchSize(10);
55+
m.Cascade(Mapping.ByCode.Cascade.All);
56+
},
57+
xr => xr.OneToMany());
58+
});
59+
60+
mapper.Class<EntityComponentId>(
61+
rc =>
62+
{
63+
rc.ComponentAsId(
64+
x => x.Id,
65+
m =>
66+
{
67+
m.Property(t => t.Id1);
68+
m.Property(e => e.Id2);
69+
}
70+
);
71+
rc.Property(x => x.Name);
72+
rc.BatchSize(3);
73+
rc.Bag(
74+
x => x.Children,
75+
m =>
76+
{
77+
m.Key(
78+
km =>
79+
{
80+
km.Columns(x => x.Name("ParentId1"), x => x.Name("ParentId2"));
81+
km.ForeignKey("none");
82+
});
83+
m.BatchSize(10);
84+
m.Cascade(Mapping.ByCode.Cascade.All);
85+
},
86+
xr => xr.OneToMany());
87+
});
88+
89+
mapper.Class<Child>(
90+
rc =>
91+
{
92+
rc.Id(m => m.Id, m => m.Generator(Generators.Guid));
93+
rc.Property(m => m.Name);
94+
rc.Property(m => m.ParentId);
95+
});
96+
97+
mapper.Class<ChildForComponent>(
98+
rc =>
99+
{
100+
rc.Id(m => m.Id, m => m.Generator(Generators.Guid));
101+
rc.Property(m => m.Name);
102+
rc.Property(m => m.ParentId1);
103+
rc.Property(m => m.ParentId2);
104+
});
105+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)