Skip to content

Commit ce18831

Browse files
committed
Add test to show bug fix works with second level cache.
1 parent c83bfb3 commit ce18831

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH3634
2+
{
3+
class CachedPerson
4+
{
5+
public virtual int Id { get; set; }
6+
public virtual string Name { get; set; }
7+
public virtual Connection Connection { get; set; }
8+
}
9+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using NHibernate.Mapping.ByCode;
2+
using NHibernate.Mapping.ByCode.Conformist;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3634
5+
{
6+
class CachedPersonMapper : ClassMapping<CachedPerson>
7+
{
8+
public CachedPersonMapper()
9+
{
10+
Id(p => p.Id, m => m.Generator(Generators.Identity));
11+
Lazy(false);
12+
Cache(m => m.Usage(CacheUsage.ReadWrite));
13+
Table("cachedpeople");
14+
Property(p => p.Name);
15+
Component(
16+
p => p.Connection,
17+
m =>
18+
{
19+
m.Class<Connection>();
20+
m.Property(c => c.ConnectionType, mapper => mapper.NotNullable(true));
21+
m.Property(c => c.Address, mapper => mapper.NotNullable(false));
22+
m.Property(c => c.PortName, mapper => mapper.NotNullable(false));
23+
});
24+
}
25+
}
26+
}

src/NHibernate.Test/NHSpecificTest/NH3634/FixtureByCode.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ protected override HbmMapping GetMappings()
1111
{
1212
var mapper = new ModelMapper();
1313
mapper.AddMapping<PersonMapper>();
14+
mapper.AddMapping<CachedPersonMapper>();
1415

1516
return mapper.CompileMappingForAllExplicitlyAddedEntities();
1617
}
@@ -45,8 +46,21 @@ protected override void OnSetUp()
4546
};
4647
session.Save(e2);
4748

49+
var cachedConnection = new Connection
50+
{
51+
Address = "test.com",
52+
ConnectionType = "http",
53+
};
54+
var cachedPerson = new CachedPerson
55+
{
56+
Name = "Cached",
57+
Connection = cachedConnection
58+
};
59+
session.Save(cachedPerson);
60+
4861
session.Flush();
4962
transaction.Commit();
63+
session.Evict(typeof(CachedPerson));
5064
}
5165
}
5266

@@ -131,6 +145,53 @@ public void QueryAgainstComponentWithANullPropertyUsingCriteria()
131145
}
132146
}
133147

148+
[Test]
149+
public void CachedQueryAgainstComponentWithANullPropertyUsingCriteria()
150+
{
151+
var componentToCompare = new Connection
152+
{
153+
ConnectionType = "http",
154+
Address = "test.com",
155+
PortName = null
156+
};
157+
158+
using (ISession session = OpenSession())
159+
using (ITransaction tx = session.BeginTransaction())
160+
{
161+
var cached = session.CreateCriteria<CachedPerson>()
162+
.Add(Restrictions.Eq("Connection", componentToCompare))
163+
.SetCacheable(true)
164+
.UniqueResult<CachedPerson>();
165+
166+
Assert.That(cached.Name, Is.EqualTo("Cached"));
167+
Assert.That(cached.Connection.PortName, Is.Null);
168+
169+
using (var dbCommand = session.Connection.CreateCommand())
170+
{
171+
dbCommand.CommandText = "DELETE FROM cachedpeople";
172+
tx.Enlist(dbCommand);
173+
dbCommand.ExecuteNonQuery();
174+
}
175+
176+
tx.Commit();
177+
}
178+
179+
using (ISession session = OpenSession())
180+
using (ITransaction tx = session.BeginTransaction())
181+
{
182+
//Should retreive from cache since we deleted directly from database.
183+
var cached = session.CreateCriteria<CachedPerson>()
184+
.Add(Restrictions.Eq("Connection", componentToCompare))
185+
.SetCacheable(true)
186+
.UniqueResult<CachedPerson>();
187+
188+
Assert.That(cached.Name, Is.EqualTo("Cached"));
189+
Assert.That(cached.Connection.PortName, Is.Null);
190+
191+
tx.Commit();
192+
}
193+
}
194+
134195
[Test]
135196
public void QueryOverANullComponentProperty()
136197
{

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,8 @@
809809
<Compile Include="NHSpecificTest\NH3604\Entity.cs" />
810810
<Compile Include="NHSpecificTest\NH3604\FixtureByCode.cs" />
811811
<Compile Include="NHSpecificTest\NH3634\Person.cs" />
812+
<Compile Include="NHSpecificTest\NH3634\CachedPerson.cs" />
813+
<Compile Include="NHSpecificTest\NH3634\CachedPersonMapper.cs" />
812814
<Compile Include="NHSpecificTest\NH3634\PersonMapper.cs" />
813815
<Compile Include="NHSpecificTest\NH646\Domain.cs" />
814816
<Compile Include="NHSpecificTest\NH646\Fixture.cs" />

0 commit comments

Comments
 (0)