Hi:
The following Criteria query returns a list which may contain the same Employee multiple times.
ICriteria query = SessionManager.CurrentSession.CreateCriteria(typeof(Employee));
query = query.Add(Expression.Like("LastName", m_strQueryString + "%"))
.Add(Expression.Like("FirstName", m_strQueryString + "%"));
query = query.AddOrder(Order.Asc("LastName"))
.AddOrder(Order.Asc("FirstName"));
query = query.CreateCriteria("EmployeeResourceUnits")
.CreateCriteria("Department")
.Add(Expression.Eq("DepartmentCode", "AAADM"));
System.Collections.IList queryResults = query.List();
Employee has a one-to-many set association of EmployeeResourceUnits as follows
<set name="EmployeeResourceUnits">
<key column="EmployeeId"/>
<one-to-many class="EmployeeResourceUnit"/>
</set>
EmployeeResourceUnit has a many-to-one association with Department
<class name="EmployeeResourceUnit" table="AddressBook.EmployeeResourceUnitPeopleImportView">
<cache usage="read-only"/>
<id type="Int32" name="Id" column="EmployeeResourceUnitId" unsaved-value="0">
<generator class="identity"/>
</id>
<many-to-one name="BusinessUnit" class="BusinessUnit" column="BusinessUnitId"/>
<many-to-one name="Department" class="Department" column="DepartmentId"/>
<property name="StartDate"/>
</class>
If an Employee has more than one EmployeeResourceUnit that matches the criterion DepartmentCode = 'AAADM', it appears once in the list for each EmployeeResourceUnit. That is, there is a list item for each row returned by the generated query.
Is this correct behavior, or should the items returned by query.List() be unique?
Hibernate version:
1.2.1
Code between sessionFactory.openSession() and session.close():
ICriteria query = SessionManager.CurrentSession.CreateCriteria(typeof(Employee));
query = query.Add(Expression.Like("LastName", m_strQueryString + "%"))
.Add(Expression.Like("FirstName", m_strQueryString + "%"));
query = query.AddOrder(Order.Asc("LastName"))
.AddOrder(Order.Asc("FirstName"));
query = query.CreateCriteria("EmployeeResourceUnits")
.CreateCriteria("Department")
.Add(Expression.Eq("DepartmentCode", "AAADM"));
System.Collections.IList queryResults = query.List();
Name and version of the database you are using:
SQL/Server 2005
_________________ Mike Abraham
|