Skip to content

Difference between CreateCriteria and CreateAlias

February 13, 2013

Today, I had looked into the common issue of Inner joins in NHibernate, and the scenario of when to use CreateCriteria and Create Alias.

The differences between the two are that they are essentially the same in NHibernate and HIbernate with one fundamental difference in its application. The only difference is that CreateCriteria has 2 additional overloads without the alias parameter, this difference is long gone in the latest versions. But essentially the application is slightly difference in its usage is that CreateCriteria uses its relations of mapping from parent to child, whilst CreateAlias you defined them with your customised alias names form the root.

An alias can be defined in terms of another alias, so your first example can be written as:

// Java
Criteria criteria = session.createCriteria(Student.class)
    .createAlias("courses", "course")
    .createAlias("course.group", "student")
    .add(Restrictions.eq("course.name", "Math"))
    .add(Restrictions.eq("student.name", "John"));

// C#
ICriteria criteria = session.CreateCriteria<Student>()
    .CreateAlias("Courses", "course")
    .CreateAlias("course.Group", "student")
    .Add(Restrictions.Eq("course.Name", "Math"))
    .Add(Restrictions.Eq("student.Name", "John"));

If you also look at an example I described in a previous post, looking at the LoadSuppliersBy with the addition of the parameter of companyName from the Suppliers table, you need to do two inner joins in one query, using CreateAlias you can easily do this as:-

public List<Supplier> LoadSuppliersBy(string productCategoryName, string companyName) {
ICriteria criteria = Session.CreateCriteria(typeof(Supplier))
.CreateAlias(“Products”, “product”)
.CreateAlias(“product.Category”, “productCategory”)
.Add(Expression.Eq(“productCategory.Name”, productCategoryName))

.CreateAlias(“product.Suppliers”, “productSuppliers”)

.Add(Expression.Eq(“productSuppliers.CompanyName”, companyName))
.SetResultTransformer(new DistinctRootEntityResultTransformer());
return criteria.List<Supplier>() as List<Supplier>;
}

So you can see here that the two joins are for matching the product category name from the Products (one to many bag) and inside that its Category many to one object with the field Name. The second join is from the Products (one to many bag) and inside that its Suppliers many to one object and you get the field Company Name. This is using Create Alias.

You can’t do this in CreateCriteria however if you replaced the wording of CreateAlias, it would refer throw an error as it would refer from its current mapping level starting from products then would not understand the wording “product.Category”, it uses the name itself from the XML mapping, so it would be Category instead. From there, it would then continue from that mapping level, so you have to write Name for the category name in the expression. If you write another CreateCriteria query, it would try and find any many to one mappings from the Category class inside Product. So that’s the difference.

 

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: