Skip to content

Commit a1204b0

Browse files
committed
Merge pull request #4 from NikGovorov/NH-2489
Addition to NH-2489. (NH-2881)
2 parents 8c4f0e3 + fb8cf9f commit a1204b0

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ public MapScenario(ISessionFactory factory)
6262
{"Child1", new Child()},
6363
{"NullChild", null},
6464
};
65+
66+
var child1 = new AnotherChild { Name = "AnotherChild1" };
67+
var child2 = new AnotherChild { Name = "AnotherChild2" };
68+
69+
s.Save(child1);
70+
s.Save(child2);
71+
72+
entity.OneToManyNamedChildren = new Dictionary<string, AnotherChild>
73+
{
74+
{"AnotherChild1" , child1},
75+
{"AnotherChild2" , child2}
76+
};
77+
6578
s.Save(entity);
6679
t.Commit();
6780
}
@@ -143,8 +156,11 @@ public void Map_Item()
143156
var entity = s.CreateQuery("from Base").UniqueResult<Base>();
144157
// null collection members don't seem to work, at least for lazy="extra" collections
145158
entity.NamedChildren.Count.Should().Be.EqualTo(2);
159+
entity.OneToManyNamedChildren.Count.Should().Be.EqualTo(2);
146160
NHibernateUtil.IsInitialized(entity.NamedChildren).Should().Be.False();
147161
Executing.This(() => { Child ignored = entity.NamedChildren["InvalidKey"]; }).Should().Throw<KeyNotFoundException>();
162+
Executing.This(() => { AnotherChild ignored = entity.OneToManyNamedChildren["InvalidKey"]; }).Should().Throw<KeyNotFoundException>();
163+
NHibernateUtil.IsInitialized(entity.NamedChildren).Should().Be.False();
148164
}
149165
}
150166
}
@@ -167,6 +183,10 @@ public void Map_TryGetValue_Invalid()
167183
Child child;
168184
entity.NamedChildren.TryGetValue("InvalidKey", out child).Should().Be.False();
169185
child.Should().Be.Null();
186+
AnotherChild anotherChild;
187+
entity.OneToManyNamedChildren.TryGetValue("InvalidKey", out anotherChild).Should().Be.False();
188+
child.Should().Be.Null();
189+
NHibernateUtil.IsInitialized(entity.NamedChildren).Should().Be.False();
170190
}
171191
}
172192
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,26 @@
2020
<column name="Child" not-null="false" />
2121
</many-to-many>
2222
</map>
23+
24+
<map name="OneToManyNamedChildren" lazy="extra" cascade="all" >
25+
<key column="Bid" />
26+
<index column="Name" type="string" />
27+
<one-to-many class="AnotherChild"/>
28+
</map>
2329
</class>
2430

2531
<class name="Child">
2632
<id name="Id">
2733
<generator class="increment"/>
2834
</id>
2935
</class>
30-
36+
37+
<class name="AnotherChild">
38+
<id name="Id">
39+
<generator class="increment"/>
40+
</id>
41+
<property name="Name"></property>
42+
</class>
43+
44+
3145
</hibernate-mapping>

src/NHibernate.Test/NHSpecificTest/NH2489/Model.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public virtual IDictionary<string, Child> NamedChildren
2121
get;
2222
set;
2323
}
24+
25+
public virtual IDictionary<string, AnotherChild> OneToManyNamedChildren
26+
{
27+
get;
28+
set;
29+
}
2430
}
2531

2632
public class Child
@@ -31,4 +37,19 @@ public virtual int Id
3137
set;
3238
}
3339
}
40+
41+
public class AnotherChild
42+
{
43+
public virtual int Id
44+
{
45+
get;
46+
set;
47+
}
48+
49+
public virtual string Name
50+
{
51+
get;
52+
set;
53+
}
54+
}
3455
}

src/NHibernate/Persister/Collection/OneToManyPersister.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ protected override ICollectionInitializer CreateSubselectInitializer(
388388

389389
public override object GetElementByIndex(object key, object index, ISessionImplementor session, object owner)
390390
{
391-
return new CollectionElementLoader(this, Factory, session.EnabledFilters).LoadElement(session, key, IncrementIndexByBase(index));
391+
return new CollectionElementLoader(this, Factory, session.EnabledFilters).LoadElement(session, key, IncrementIndexByBase(index)) ?? NotFoundObject;
392392
}
393393
#region NH Specific
394394
protected override SqlCommandInfo GenerateIdentityInsertRowString()

0 commit comments

Comments
 (0)