Skip to content

Commit 4f4343e

Browse files
committed
Add ability to set fetch for <one-to-one> mapping in mapping by code
1 parent e26af46 commit 4f4343e

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/NHibernate.Test/MappingByCode/MappersTests/OneToOneMapperTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,31 @@ public void WhenSetFormulaWithMultipleLinesThenSetFormulaNode()
164164
Assert.That(hbmFormula.Text[1], Is.EqualTo("Line2"));
165165
Assert.That(mapping.formula1, Is.Null);
166166
}
167+
168+
[Test]
169+
public void WhenSetFetchModeToJoinThenSetFetch()
170+
{
171+
var member = For<MyClass>.Property(c => c.Relation);
172+
var mapping = new HbmOneToOne();
173+
var mapper = new OneToOneMapper(member, mapping);
174+
175+
mapper.Fetch(FetchKind.Join);
176+
177+
Assert.That(mapping.fetch, Is.EqualTo(HbmFetchMode.Join));
178+
Assert.That(mapping.fetchSpecified, Is.True);
179+
}
180+
181+
[Test]
182+
public void WhenSetFetchModeToSelectThenSetFetch()
183+
{
184+
var member = For<MyClass>.Property(c => c.Relation);
185+
var mapping = new HbmOneToOne();
186+
var mapper = new OneToOneMapper(member, mapping);
187+
188+
mapper.Fetch(FetchKind.Select);
189+
190+
Assert.That(mapping.fetch, Is.EqualTo(HbmFetchMode.Select));
191+
Assert.That(mapping.fetchSpecified, Is.True);
192+
}
167193
}
168194
}

src/NHibernate/Mapping/ByCode/IOneToOneMapper.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public interface IOneToOneMapper : IEntityPropertyMapper
1515
void Formula(string formula);
1616
void ForeignKey(string foreignKeyName);
1717
void Class(System.Type clazz);
18+
19+
//6.0 TODO: Uncomment
20+
//void Fetch(FetchKind fetchMode);
1821
}
1922

2023
public interface IOneToOneMapper<T> : IOneToOneMapper
@@ -35,5 +38,11 @@ public static void Formulas(this IOneToOneMapper mapper, params string[] formula
3538
var o2oMapper = ReflectHelper.CastOrThrow<OneToOneMapper>(mapper, "Setting many formula");
3639
o2oMapper.Formulas(formulas);
3740
}
41+
42+
public static void Fetch(this IOneToOneMapper mapper, FetchKind fetchMode)
43+
{
44+
var o2oMapper = ReflectHelper.CastOrThrow<OneToOneMapper>(mapper, "Setting fetch");
45+
o2oMapper.Fetch(fetchMode);
46+
}
3847
}
3948
}

src/NHibernate/Mapping/ByCode/Impl/OneToOneMapper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ public void ForeignKey(string foreignKeyName)
135135
}
136136

137137
#endregion
138+
139+
public void Fetch(FetchKind fetchMode)
140+
{
141+
_oneToOne.fetch = fetchMode.ToHbm();
142+
_oneToOne.fetchSpecified = true;
143+
}
138144
}
139145

140146
public class OneToOneMapper<T> : OneToOneMapper, IOneToOneMapper<T>

0 commit comments

Comments
 (0)