I have encountered some problems using ICriteria with associations with the RC1 release. I am trying to return all Orders with a particular instance of Customer, where Customer is a many-to-one association in the Order class.
Extract from mapping files:
Code:
<class name="Customer" table="Customer">
<id name="Id" unsaved-value="0">
<generator class="identity" />
</id>
...
</class>
<class name="Order" table="`Order`">
<id name="Id" unsaved-value="0">
<generator class="identity" />
</id>
<many-to-one name="Customer" column="CustomerID" class="Customer" />
...
</class>
The following code works with 0.9.1.0:
Code:
IList ordersForCustomer = session.CreateCriteria(typeof(Order))
.Add(Expression.Eq("Customer", customer))
.List();
In RC1, this throws the a QueryException with the message
Code:
cannot use association or collection (MyTests.Order.Customer) directly in a criterion, use ICriteria.CreateCriteria instead
I can make this work by using the following
Code:
ICriteria criteria = session.CreateCriteria(typeof(Order));
criteria.CreateCriteria("Customer")
.Add(Expression.Eq("Id", customer.Id));
IList ordersForCustomer = criteria.List();
I would prefer to be able to use the first syntax for a number of reasons.
- The second is more long-winded.
- The first is more instinctive and more object-oriented. I want to look for orders that have a particular Customer object, but I am being forced to look for orders that have a Customer object with a particular Id property.
- I have a number of instances in my code with the first syntax and I'd rather not have to change them all!
Two questions:
- Is there a reason why the old syntax is no longer supported?
- Is there a better way to achieve what I'm attempting?