Hello, Im currently doing my bachelor project for a company here in Norway with 3 other students.
We decided to use
nHibernate, and have run into some trouble.
We are all new to
nHibernate, and I have been given the main task of working with
nHibernate.
The problem seems to happen when we are trying to get a generic
IList from an
ICriteria using
List<>().
We are adding an
Example with the desirable fields to the
ICriteria,
but when the table has a
many-to-many relation, it always returns an empty list.
Because it was a
many-to-many relation we added a generic
ISet in the code.
Without the
ISet in the class
List<>() returns correct, but with
ISet it always returns nothing.
This is how we are doing it:
Lets say we have two classes/tables
Employee and
Position.
An
Employee can have one or many different
positions,
and one
Position can be taken by one or many
employees, which gives us a
many-to-many relation.
Code:
Code:
public class DTOEmployee
{
public virtual Int32 ID { get; set; }
public virtual ISet<DTOEmployeePostion> Positions { get; set; }
}
public class DTOPosition
{
public virtual Int32 ID { get; set; }
public virtual ISet<DTOEmployeePosition> Employees { get; set; }
}
public class DTOEmployeePosition
{
public virtual Int32 ID { get; set; }
public virtual DTOEmployee Employee { get; set; }
public virtual DTOPosition Position { get; set; }
public virtual Nullable<DateTime> Date { get; set; }
}
Mapping documents:Code:
<class name="DTOEmployee" table="Employee">
<id name="ID" type="Int32">
<generator class="identity"/>
</id>
<set name="Positions">
<key column="ID"/>
<one-to-many class="DTOEmployeesPosition"/>
</set>
</class>
<class name="DTOPosition" table="Position">
<id name="ID" type="Int32">>
<generator class="identity"/>
</id>
<set name="Employees">
<key column="ID"/>
<one-to-many class="DTOEmployeesPosition"/>
</set>
</class>
<class name="DTOEmployeesPosition" table="EmployeesPosition">
<id name="ID" type="Int32">>
<generator class="identity"/>
</id>
<property name="Date" type="DateTime"/>
</class>