Skip to content

Commit ab23e13

Browse files
David EllingsworthDavid Ellingsworth
David Ellingsworth
authored and
David Ellingsworth
committed
GH2552 Add tests to verify that the cache is updated on delete.
1 parent 9e6944a commit ab23e13

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

src/NHibernate.Test/NHSpecificTest/GH2552/Fixture.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,79 @@ protected override void OnTearDown()
6565
Assert.AreEqual(0, statistics.SecondLevelCacheMissCount, "Second level cache miss count");
6666
}
6767

68+
private void OneToOneUpdateTest<TPerson, TDetails>() where TPerson : Person, new() where TDetails : Details, new()
69+
{
70+
List<object> ids = this.CreatePersonAndDetails<TPerson, TDetails>();
71+
72+
IStatistics statistics = Sfi.Statistics;
73+
74+
// Clear the second level cache and the statistics
75+
Sfi.EvictEntity(typeof(TPerson).FullName);
76+
Sfi.EvictEntity(typeof(TDetails).FullName);
77+
Sfi.EvictQueries();
78+
79+
statistics.Clear();
80+
81+
// Fill the empty caches with data.
82+
this.FetchPeopleById<TPerson>(ids);
83+
84+
// Verify that no data was retrieved from the cache.
85+
Assert.AreEqual(0, statistics.SecondLevelCacheHitCount, "Second level cache hit count");
86+
statistics.Clear();
87+
88+
int personId = DeleteDetailsFromFirstPerson<TPerson>();
89+
90+
// Verify that the cache was updated
91+
Assert.AreEqual(1, statistics.SecondLevelCachePutCount, "Second level cache put count");
92+
statistics.Clear();
93+
94+
// Verify that the Person was updated in the cache
95+
using (ISession s = Sfi.OpenSession())
96+
using (ITransaction tx = s.BeginTransaction())
97+
{
98+
TPerson person = s.Get<TPerson>(personId);
99+
100+
Assert.IsNull(person.Details);
101+
}
102+
103+
Assert.AreEqual(0, statistics.SecondLevelCacheMissCount, "Second level cache miss count");
104+
statistics.Clear();
105+
106+
// Verify that the Details was removed from the cache and deleted.
107+
using (ISession s = Sfi.OpenSession())
108+
using (ITransaction tx = s.BeginTransaction())
109+
{
110+
TDetails details = s.Get<TDetails>(personId);
111+
112+
Assert.Null(details);
113+
}
114+
115+
Assert.AreEqual(0, statistics.SecondLevelCacheHitCount, "Second level cache hit count");
116+
}
117+
118+
private int DeleteDetailsFromFirstPerson<TPerson>() where TPerson:Person
119+
{
120+
using (ISession s = Sfi.OpenSession())
121+
using (ITransaction tx = s.BeginTransaction())
122+
{
123+
// Get the first person with details.
124+
Person person = s.QueryOver<TPerson>()
125+
.Where(p => p.Details != null)
126+
.Take(1)
127+
.SingleOrDefault();
128+
129+
Assert.NotNull(person);
130+
Assert.NotNull(person.Details);
131+
132+
s.SaveOrUpdate(person);
133+
person.Details = null;
134+
135+
tx.Commit();
136+
137+
return person.Id;
138+
}
139+
}
140+
68141
private List<object> CreatePersonAndDetails<TPerson, TDetails>() where TPerson : Person, new() where TDetails : Details, new()
69142
{
70143
List<object> ids = new List<object>();
@@ -125,5 +198,17 @@ public void OneToOneCacheFetchByRef()
125198
{
126199
OneToOneFetchTest<PersonByRef, DetailsByRef>();
127200
}
201+
202+
[Test]
203+
public void OneToOneCacheUpdateByForeignKey()
204+
{
205+
OneToOneUpdateTest<PersonByFK, DetailsByFK>();
206+
}
207+
208+
[Test]
209+
public void OneToOneCacheUpdateByRef()
210+
{
211+
OneToOneUpdateTest<PersonByRef, DetailsByRef>();
212+
}
128213
}
129214
}

src/NHibernate.Test/NHSpecificTest/GH2552/Mappings.hbm.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.GH2552">
33
<class name="PersonByFK" batch-size="3">
4-
<cache usage="read-only"/>
4+
<cache usage="read-write"/>
55
<id name="Id" unsaved-value="0" generator="native"/>
66
<property name="Name"/>
7-
<one-to-one name="Details" class="DetailsByFK" fetch="join" cascade="all"/>
7+
<one-to-one name="Details" class="DetailsByFK" fetch="join" cascade="all-delete-orphan"/>
88
</class>
99

1010
<class name="DetailsByFK">
11-
<cache usage="read-only"/>
11+
<cache usage="read-write"/>
1212
<id name="Id" unsaved-value="0">
1313
<generator class="foreign">
1414
<param name="property">Person</param>
@@ -19,14 +19,14 @@
1919
</class>
2020

2121
<class name="PersonByRef" batch-size="3">
22-
<cache usage="read-only"/>
22+
<cache usage="read-write"/>
2323
<id name="Id" unsaved-value="0" generator="native"/>
2424
<property name="Name" />
25-
<one-to-one name="Details" class="DetailsByRef" property-ref="Person" fetch="join" cascade="all"/>
25+
<one-to-one name="Details" class="DetailsByRef" property-ref="Person" fetch="join" cascade="all-delete-orphan"/>
2626
</class>
2727

2828
<class name="DetailsByRef">
29-
<cache usage="read-only"/>
29+
<cache usage="read-write"/>
3030
<id name="Id" unsaved-value="0" generator="native"/>
3131
<many-to-one name="Person" class="PersonByRef" column="PersonId" unique="true"/>
3232
<property name="Data"/>

0 commit comments

Comments
 (0)