Hi Gilles,
My case is very complex and would be impractical to try and explain, because
of this I sat down this morning and tried to distil the problem right down
to its simplest reproducible form. What do you know, it disappeared! I could
select objects whose child lists were empty. I started picking through and
adding little aspects of complexity to try and reproduce the error and
almost immediately I realised that selecting objects with empty lists was
fine until I added inheritance mapping on the child objects.
My example code goes like this:
public class Parent
{
protected Nullable<Int32> id = null;
public virtual Nullable<Int32> Id
{
get { return id; }
set { id = value; }
}
protected IList<Child> children = new List<Child>();
public IList<Child> Children
{
get { return children; }
}
}
public class Child
{
protected Nullable<Int32> id = null;
public virtual Nullable<Int32> Id
{
get { return id; }
set { id = value; }
}
}
<resultMaps>
<resultMap id="child" class="Child">
<result property="id" column="Child_ID"/>
</resultMap>
<resultMap id="parent" class="Parent" groupBy="id">
<result property="id" column="Parent_ID"/>
<result property="children" resultMapping="Item.child"/>
</resultMap>
</resultMaps>
The SQL involved is a simple join between two tables, Parent and Child, the
primary key of Parent being a foreign key in Child. The above code works
perfectly, empty Children lists give no exception.
Broken Code:
public class Boy : Child
{
}
public class Girl : Child
{
}
<resultMaps>
<resultMap id="child" class="Child">
<result property="id" column="Child_ID"/>
<discriminator column="Child_Subclass" type="String"/>
<subMap value="Boy" resultMapping="boy"/>
<subMap value="Girl" resultMapping="girl"/>
</resultMap>
<resultMap id="girl" class="Girl"/>
<resultMap id="boy" class="Boy"/>
<resultMap id="parent" class="Parent" groupBy="id">
<result property="id" column="Parent_ID"/>
<result property="children" resultMapping="Item.child"/>
</resultMap>
</resultMaps>
When the code is refactored as above, we encounter the
NullReferenceException I spoke of in my first mail with empty lists. I hope
this helps clear this up.
Regards,
Sean