I've an object containing a collection:
public class Teacher
{
public virtual string Name {get;set;}
public virtual ISet<Student> Students {get;set;}
// ...
}
public class Student
{
public virtual string LastName {get;set;}
// ...
}
The DetachedCriteria of type "Teacher" populates correctly the "Teacher" object with a collection of the mapped students.
Example:
If teacher 'X' contains 3 students 'A', 'B' and 'C': one object (the 'Teacher') is returned containing a collection of 3 objects (students 'A', 'B' and 'C').
The mapping configuration is:
<class table="Teacher" name="...">
<id name="Id" ... />
<property name="Name" column="Name" />
<set name="Students" table="Student" inverse="true">
<key column="TeacherId" />
<one-to-many class="Student" />
</set>
</class>
I want to retrieve from the database one line/student. I've created a new class as follow:
public class TeacherWithFlattenCollection
{
public virtual string Name {get;set;} // The name of the teacher
public virtual string LastName {get;set;} // The name of the student
}
The mapping configuration is:
<class table="Teacher" name="...">
<id name="Id" ... />
<property name="Name" column="Name" />
<join table="Student" fetch="join">
<key column="TeacherId" />
<property name="LastName" column="LastName" />
</join>
</class>
By using this new class for the DetachedCriteria, I receive the correct number of items but they are not correctly populated:
- Name = 'X', LastName = 'A'
- Name = 'X', LastName = 'A'
- Name = 'X', LastName = 'A'
Instead of:
- Name = 'X', LastName = 'A'
- Name = 'X', LastName = 'B'
- Name = 'X', LastName = 'C'
Do I miss something in the mapping configuration?