It was code cleanup day. So time to get rid of some nagging warnings I didn’t had time to tackle before. One of the warnings I wanted to get rid of was an NHibernate warning about the fact the CreateMultiCriteria method was now obsolete.
This is how my code looked like originally:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var lastNameMatches = Restrictions.Like("LastName", "A", MatchMode.Anywhere); | |
var shipCityMatches = Restrictions.Eq("ShipCity", "Madrid"); | |
var employeeCriteria = DetachedCriteria | |
.For<Employee>() | |
.SetResultTransformer(Transformers.DistinctRootEntity); | |
var orderCriteria = DetachedCriteria | |
.For<Employee>() | |
.CreateCriteria("Orders", "Orders", JoinType.LeftOuterJoin) | |
.Add(shipCityMatches); | |
var employeeRelationCriteria = DetachedCriteria | |
.For<Employee>() | |
.CreateAlias("Employees", "Employees", JoinType.LeftOuterJoin) | |
.Add(lastNameMatches); | |
var results = Session | |
.CreateMultiCriteria() | |
.Add(employeeCriteria) | |
.Add(orderCriteria) | |
.Add(employeeRelationCriteria) | |
.List(); |
And this how I got rid of the CreateMultiCriteria message:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var lastNameMatches = Restrictions.Like("LastName", "A", MatchMode.Anywhere); | |
var shipCityMatches = Restrictions.Eq("ShipCity", "Madrid"); | |
var employeeCriteria = DetachedCriteria | |
.For<Employee>() | |
.SetResultTransformer(Transformers.DistinctRootEntity); | |
var orderCriteria = DetachedCriteria | |
.For<Employee>() | |
.CreateCriteria("Orders", "Orders", JoinType.LeftOuterJoin) | |
.Add(shipCityMatches); | |
var employeeRelationCriteria = DetachedCriteria | |
.For<Employee>() | |
.CreateAlias("Employees", "Employees", JoinType.LeftOuterJoin) | |
.Add(lastNameMatches); | |
var results = session | |
.CreateQueryBatch() | |
.Add<Employee>(employeeCriteria) | |
.Add<Employee>(orderCriteria) | |
.Add<Employee>(employeeRelationCriteria) | |
.GetResult<Employee>(1); |
Notice that I replaced the CreateMultiCriteria call with a CreateQueryBatch call. The API is a little bit different. Most important to notice is that there is a GetResult method where you can specify what call result you want to get back.